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
To create a map with ggplot, use geom_sf for each layer
We’ll add three new packages to help with styles:
scales: to format text in our labelsviridis: 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 designersLet’s create a chloropleth map using ggplot.
Create a new script, called ga_chloropleth_map
Add the necessary packages
Import our school district spatial dataframe
Create a ggplot map
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:
To select our color scheme and format the legend titles and labels we need two more packages:
scale_fill_viridis()scales:: percent_format()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
We adjusted the line color outside of the aesthetic.
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))
<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 adjustedCreate your color palette
I highly recommend 0to255 to create color palettes from any color
Define the in your data with the
breaksparameterUse
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 "
)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.
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().
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
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)Graphic designers want maps as vectors they can manipulate them. You can export .svgs with ggsave().
svglite packageCopy your final ggplot code chunk to make a new ggplot object
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() Use
ggsave()to save your new object