Skip to content
R for the Rest of Us Logo

Mapping with R

Faceting Choropleth Maps with ggplot2 (02_08)

Transcript

Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.

Your Turn

Open project 02_08.

Recreate this facetted choropleth from the your-turn.R script.

  • Create a vector containing the correct order of educational attainments

  • Use fct_relevel() to reorder the facet labels.

Learn More

As mentioned in the video, small multiples is a data visualization technique that {ggplot2} makes really easy with the facet_wrap() and facet_grid() functions. The facet_grid() function allows you to facet by two categorical variables, eg

library(tidyverse)
library(palmerpenguins)
penguins %>%
  ggplot(aes(x = bill_length_mm,
             y = bill_depth_mm)) +
  geom_point() +
  facet_grid(species ~ island) +
  theme_gray(base_size = 16)

It's also possible to do this two-level faceting for your choropleth!

The solution I've given for the your turn example isn't completely perfect because the facet labels are slightly cut-off - take a look at the bottom of the g's! To fix this you need to know that the facet labels are called "strip" in the {ggplot2} theme and you can tidy up these labels as follows:

gg_faceted_choropleth +
  theme_void() +
  theme(strip.text.x = element_text(margin = margin(b = 1, t = 0)))

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.

Course Content

34 Lessons