How to use R to make a Business Reporting Map

Lin Zhu
Geek Culture
Published in
4 min readApr 19, 2023
Photo by Emile Perron on Unsplash

In many business reports, a clear map visualization is required to support illustrating the information such as How are the sales of different stores in the city, where we have the most customers.

There are several BI software programs that provide solutions on how to create such maps. But they are always expensive and have a high learning curve. However, once we are able to program to a certain extent, it’s easy and cheap to create the maps just in several steps without GIS knowledge.

One typical map type is the bubble map. Bubble Maps use different size circles to present the numeric values of geographical locations (e.g. coordinates or regions).

In my article Munich Business Map (2023): A Guide to top employers in Munich, I used bubble maps to illustrate the revenues of top companies in Munchen. These maps are created based on R and use an Excel sheet as data input. In this article, I will use the map I created in the above article as an example and talk about how we could map out our business information simply using R.

step 1: Load libraries

There are several packages are required to be loaded.

sf: Support for simple features, a standardized way to encode spatial vector data.

dplyr: dplyr provides a consistent set of functions for most manipulations such as select, filter, and mutate.

readxl: helps to read data out of Excel easily.

ggmap: contains a collection of functions for visualizing geospatial data and models from online mapping services (google map & stamen map) using ggplot framework.

ggrepel: provides a function to repel overlapping text labels for geometry elements in ggplot.

# load libraries
library(sf)
library(dplyr)
library(readxl)
library(ggmap)
library(ggrepel)

Step 2: Read in Datasets

Here we have two types of datasets

  1. Excel sheet: contains the revenue values & coordinates details of companies. click here to get the input excel data.
  2. Static Map tiles: it is requested via get_stamenmap() function from ggmap package. the requested map will be used as basemap.
data <- read_excel("D:/CityMap/Munchen/company_hub/code/company.xlsx", sheet = "fulllist")
data_sf <- st_as_sf(data, coords = c("lng", "lat"), crs = 4326)
data_sf_manufacture <- data %>% filter(Category %in% c("Manufacture", "Property"))

bbx <- st_bbox(data_sf)
mymap <- get_stamenmap(bbox = c(left = 11.27, bottom = 48.02, right = 11.8, top =48.28644),
zoom=12, maptype = "toner-lite")

The get_stamenmap() function asks for the bounding box information of the requested city. here, you can find a way on How to get a bounding box

library(osmdata)
bbx = getbb("München")

Step 3: Set Map Template

Before plotting the data to map, we could set the map template first. The map template helps define styles such as font, background color, legend position, etc. Such a template can also be reused in other mapping work.

theme_map <- function(...) {
theme_minimal() +
theme(
text = element_text(family = "Noto-Sans", color = "#22211d"),
# remove all axes
axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),

# background colors
plot.background = element_rect(fill = "white",
color = NA),
panel.background = element_rect(fill = "white",
color = NA),
legend.background = element_rect(fill = "white",
color = NA),
# legend
legend.position = c(.95, .2),
legend.text = element_text( size = 25),
legend.title = element_text( size = 30),
legend.spacing.y = unit(0, 'cm'),
legend.spacing.x = unit(0, 'cm')

# title
plot.title = element_text(size = 50, hjust = 0.5,
color = "#4e4d47"
),
plot.subtitle = element_text(size = 30, hjust = 0.5,
color = "#4e4d47",

debug = F),
# captions
plot.caption = element_text(size = 25,
hjust = .5,
margin = margin(t = 0.2,
b = 0,
unit = "cm"),
color = "#939184")
)
}

Step 4: Plot the Map

In some cases, we are not sure whether the required fonts already exist in the system. Here, we use the showtext library to simply load the custom fonts into the system, then tell R to render text using showtext by calling the showtext_auto() function.

library (showtext)
# in map template above we use font Noto Sans
font_add_google(name = "Noto Sans", family = "Noto-Sans")

# use showtext to render the plot
showtext_auto()

To plot the map, we will use the ggmap. Firstly, loading the stamen map tiles as the base map and then using the geom_point() function to read the points dataset. Here, we specifically use the geom_label_repel() function to create the non-overlapped labels for selected points.

ggmap(mymap) +
geom_point(data = data_sf_manufacture,aes(x = lng, y = lat, size = Scale),color="red",alpha=.6)+
geom_point(data = subset(data_sf_manufacture, Scale==7),aes(x = lng, y = lat,size = Scale),
color="yellow", show.legend = F, stroke = 1, shape=21)+
scale_size_continuous(range = c(2, 6), labels = c("<= 10M", "10-50M","50-100M", "100-500M","500M-1B", "1-10B",">10B"))+
labs(title="TOP COMPANIES IN MANUFACTURING",
size = "Revenue ($)",
caption ="Data Source: ZoomInfo|city of Munchen Department of Labor and Economic Development")+
geom_label_repel(data=subset(data_sf_manufacture,Scale==7),
aes(x = lng, y = lat,label=name),
size = 8,
family = "Noto Sans",
segment.curvature = -0.1,
segment.ncp = 3,
segment.angle = 20)+
theme_map()

Step 5: Save the Map

use ggsave function to save the map in png or img format. Meanwhile, the function allows users to self-define the desired image size and dpi.

ggsave(
filename = "D:/CityMap/Munchen/company_hub/manufacture.png",
width = 5, height = 4, dpi = 500,
device = "png"
)

The same code structure (Import libraries -> Read in Datasets -> Set Map Template -> Plot the Map -> Save the Map) can also be applied to other map styles such as choropleth map.

click here to access the complete code and data used in the article.

Follow me and subscribe to my newsletter to get more articles on

  1. Geo-spatial & Risk Analysis
  2. Climate Risk Insights
  3. Programming Tutorials

--

--

Lin Zhu
Geek Culture

spatial science | work in risk analysis | programmer