Static Maps

maps with ggplot


We’ll use ggplot to create and export static maps to add to a report or website.

geom_sf()

ggplot uses geom_ functions to specify the type of layer you are adding to you plot

  • There are lots of different geoms!

To create a map with ggplot, use geom_sf for each layer

extra packages

We’ll add three new packages to help with styles:

  • scales: to format text in our labels
  • viridis: color palettes designed to improve graph readability for readers with common forms of color blindness and/or color vision deficiency.
  • svglite: to export to svg for graphic designers

In-class exercise: create a map with ggplot

Let’s create a chloropleth map using ggplot.

Step 1.

Create a new script, called ga_chloropleth_map

Add the necessary packages

Import our school district spatial dataframe

library(tidyverse)
library(sf)
library(viridis)
library(scales)
library(svglite)
ga_sd_pop <- st_read("data/processed/ga_sd_pop.geojson") 

Step 2.

Create a ggplot map

ggplot()  + 
  geom_sf(data = ga_sd_pop, mapping = aes(fill = pct_bipoc_pop)) +
  theme_void()

Notice

Notice

That was super easy but there is a LOT to improve.

It makes more intuituve sense for the viewer for the color to get darker when concentrations increase. In the next iterations I’m going to:

  • reverse the direction of the color scale
  • change the color of the lines to white
  • add a title and source

To select our color scheme and format the legend titles and labels we need two more packages:

  • scale_fill_viridis()
  • scales:: percent_format()

Step 3.

Change the color of the lines to white within geom_sf()

Reverse the direction of the color scale within scale_fill_viridis()

Add a title, subtitle and source caption within labs()

ggplot()  + 
  geom_sf(data = ga_sd_pop, 
          mapping = aes(fill = pct_bipoc_pop),
          color = "#ffffff") +
  theme_void() +
  scale_fill_viridis(name="BIPOC Population (%)",
                     labels=percent_format(accuracy = 1L), 
                     direction=-1) +
  labs(
    title = "Georgia School Districts",
    subtitle = "Percent BIPOC Population",
    caption = "Source: US Census, 2020 "
  )

Notice

Notice

We adjusted the line color outside of the aesthetic.

  • Inside aes(): mapping a variable of your data
  • Inside aes(): mapping a constant

Try adding the color inside the aes() to see what happens:

geom_sf(data = ga_sd_pop,

mapping = aes(fill = pct_bipoc_pop,

color = "#000000))

Style a chloropleth with defined colors

<br<

What if I want to create my own color palette?

No problem.

  • scale_fill_stepsn() is a binned gradient color scale that is easily adjusted

In-class exercise: create a map with your own colors

Step 1.

Create your color palette

I highly recommend 0to255 to create color palettes from any color

bipoc_pink <- c("#feebe2", "#fbb4b9", "#f768a1", "#c51b8a", "#7a0177")

Step 2.

Define the in your data with the breaks parameter

Use scale_fill_stepsn() to apply your color palette

  • I find it helpful to think of the colors as going between the breaks
ggplot()  + 
  geom_sf(data = ga_sd_pop, mapping = aes(fill = pct_bipoc_pop),
          color = "#ffffff") +
  theme_void() +
  scale_fill_stepsn(breaks=c(0, .2, .4, .6, .8, 1),
                    colors = bipoc_pink, 
                    name="BIPOC Population (%)",
                    labels=percent_format(accuracy = 1L)) +
  labs(
    title = "Georgia School Districts",
    subtitle = "Percent BIPOC Population",
    caption = "Source: US Census, 2020 "
  )

Notice

Color scales

We could talk about fill color all day long. But alas! We have many other topics.

When you want more information about what all of the various scales are best for. Read this excellent post on StackOverflow.

Save your maps as png


Now that I have two maps, let’s name them so that I can call them as objects and save them to your computer with ggsave().

In-class exercise: save your map


Step 1.

Add margins to your map if you want space between the map and the edge of the png

Save your maps as objects and RUN

viridis_map <- ggplot()  + 
  geom_sf(data = ga_sd_pop, mapping = aes(fill = pct_bipoc_pop)) +
  theme_void() +
  scale_fill_viridis(name="BIPOC Population (%)",
                     labels=percent_format(accuracy = 1L), 
                     direction=-1) +
  labs(
    title = "Georgia School Districts",
    subtitle = "Percent BIPOC Population",
    caption = "Source: US Census, 2020 "
  ) +
  theme(plot.margin = margin(.5,0,.5,0, "in"))

pink_map <- ggplot()  + 
  geom_sf(data = ga_sd_pop, mapping = aes(fill = pct_bipoc_pop),
          color = "#ffffff") +
  theme_void() +
  scale_fill_stepsn(breaks=c(0, .2, .4, .6, .8, 1),
                    colors = bipoc_pink, 
                    name="BIPOC Population (%)",
                    labels=percent_format(accuracy = 1L)) +
  labs(
    title = "Georgia School Districts",
    subtitle = "Percent BIPOC Population",
    caption = "Source: US Census, 2020 "
  ) +
  theme(plot.margin = margin(.5,0,.5,0, "in"))

Use ggsave() to export your maps

Step 2.

Determine the height and width that looks best (this is trial and error usually)

Define the background color

# save ggplot object using ggsave
ggsave("ga_pop_bipoc_viridis.png", 
       plot = viridis_map, # specify the ggplot object you stored
       units = "in",
       height = 5, width = 5,
       bg = 'white')

ggsave("maps/ga_pop_bipoc_pink.png", 
       plot = pink_map, # specify the ggplot object you stored
       units = "in",
       height = 7, width = 5)

Save your maps as an svg

Graphic designers want maps as vectors they can manipulate them. You can export .svgs with ggsave().

  • you need the svglite package
  • and to make some adjustments to your map so that R will write out the lines and fill of polygons separately.

In-class exercise: export an .svg

Step 1.

Copy your final ggplot code chunk to make a new ggplot object

Step 2.

Create a polygon fill layer with no lines

Create a polygon line layer with no fill

pink_map_graphics <- ggplot()  + 
  geom_sf(data = ga_sd_pop, mapping = aes(fill = pct_bipoc_pop),
          lwd = 0) +
  scale_fill_stepsn(breaks=c(0, .2, .4, .6, .8, 1),
                    colors = bipoc_pink, 
                    name="BIPOC Population (%)",
                    labels=percent_format(accuracy = 1L)) +
  geom_sf(data = ga_sd_pop, 
          fill = "transparent",
          color = "#000000",
          lwd = .5) +
  theme_void() 
pink_map_graphics

Step 2.

Use ggsave() to save your new object

ggsave("ga_pop_bipoc_pink_graphics.svg", 
       plot = pink_map_graphics, # specify the ggplot object you stored
       units = "in",
       height = 5, width = 5)