knitr::opts_chunk$set(echo = TRUE)
pacman::p_load(
  vembedr,      # youtube embedder
  skimr,        # get overview of data
  tidyverse,    # data management + ggplot2 graphics 
  readxl,       # import excel data
  maps,
  sf,
  viridis,
  ggthemes )

Function Name: geom_map

In this document, I will introduce the geom_map function.This function is within the ggplot2 package. I will show what it’s for and show some examples of what can be done with it.

What is it for?

Geom_map is used to make polygons from a reference map. Geom_map() does the work of remembering the polygons in the data frame for you.The first call to geom_map() should (usually) be the “base layer, that has the polygon outlines and perhaps a base fill. Here is an example.

Example 1

It requires two data frames: One contains the coordinates of each polygon. The other contains the values associated with each polygon. An id variable links the two together.

ids <- factor(c("1.1", "2.1", "1.2", "2.2", "1.3", "2.3"))

values <- data.frame(
  id = ids,
  value = c(3, 3.1, 3.1, 3.2, 3.15, 3.5)
)

positions <- data.frame(
  id = rep(ids, each = 4),
  x = c(2, 1, 1.1, 2.2, 1, 0, 0.3, 1.1, 2.2, 1.1, 1.2, 2.5, 1.1, 0.3,
  0.5, 1.2, 2.5, 1.2, 1.3, 2.7, 1.2, 0.5, 0.6, 1.3),
  y = c(-0.5, 0, 1, 0.5, 0, 0.5, 1.5, 1, 0.5, 1, 2.1, 1.7, 1, 1.5,
  2.2, 2.1, 1.7, 2.1, 3.2, 2.8, 2.1, 2.2, 3.3, 3.2)
)


ggplot(values, aes(fill = value)) +
  geom_map(aes(map_id = id), map = positions) +
  expand_limits(positions) + ylim(0, 3)

Lets use geom_map using the maps package.The maps package contains a lot of outlines of continents, countries, states, and counties. We will use the geom_map function to make maps.To show examples I have loaded the maps package.

Example 2

county2 <- map_data("county")
ggplot() +
  geom_map(data=county2, map=county2, aes(x=long, y=lat, map_id=region), col="white", fill="green") +
labs(title = "Map of the US showing all it's counties",
       x = "Longitude",
       y = "Lattitude")+ theme_minimal()
## Warning in geom_map(data = county2, map = county2, aes(x = long, y = lat, :
## Ignoring unknown aesthetics: x and y

Here is a pic of the US with all the different counties.

Example 3

We can also use geom_map in conjunction with other arguments that adds layers to the picture. We get better maps when geom_map is combined with other arguments such as coord_sf.The approach coord_sf () takes is to break straight lines into small pieces (i.e., segmentize them) and then transform the pieces into projected coordinates.

set.seed(47)
county_map <- map_data("county", "oregon")
names(county_map)[5:6] <- c("state", "id")
countyData <- data.frame(id = unique(county_map$id), value = rnorm(36)) 
map1 <- ggplot(countyData, aes(map_id = id)) +
    geom_map(aes(fill = value), map = county_map, colour = "black") +
    coord_sf() +
    expand_limits(x = county_map$long, y = county_map$lat)+
labs(title = "Chloropleth map of Oregon",
       x = "Longitude",
       y = "Lattitude",) + theme_minimal()

print(map1)

Example 4 with coord_sf

crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)

# Equivalent to crimes %>% tidyr::pivot_longer(Murder:Rape)
vars <- lapply(names(crimes)[-1], function(j) {
  data.frame(state = crimes$state, variable = j, value = crimes[[j]])
})
crimes_long <- do.call("rbind", vars)

states_map <- map_data("state")


ggplot(crimes, aes(map_id = state)) +
  geom_map(aes(fill = Murder), map = states_map) +
  expand_limits(x = states_map$long, y = states_map$lat) 

# This is not a really great map. Now if we add coord_sf it looks great  

ggplot(crimes, aes(map_id = state)) +
    geom_map(aes(fill = Murder), map = states_map) +
    # crs = 5070 is a Conus Albers projection for North America,
    #   see: https://epsg.io/5070
    # default_crs = 4326 tells coord_sf() that the input map data
    #   are in longitude-latitude format
    coord_sf(
      crs = 5070, default_crs = 4326,
      xlim = c(-125, -70), ylim = c(25, 52)
    )  

Is it helpful?

Pros:

I think it’s a useful function for beginners.It really works well to make basic maps.

Cons:

It’s just the first layer of coding. Lots of additional layers of codes need to be written to make a nice map. Other functions such as geom_sf are better when it comes to mapping polygon.

Additional info: Geom_map is increasingly being replaced by geom_sf. If you want to know more about geom_sf please vist: https://ggplot2.tidyverse.org/reference/ggsf.html.

Code source: Adapted from stackoverflow.com and the tidyverse.org website.