You know it, you love it, ggplot does it all - including maps! There are a handful of packages that will be necessary for our example:
library(jsonlite)
library(rgdal)
library(sf)
# for plotting
library(extrafont)
library(ggplot2)
library(ggspatial)
library(patchwork)
library(scico)
# for data wrangling
library(dplyr)
Make sure you are in a folder containing our honey data (including the .csv, .shp, .shx, and .dbf files).
Now that we’re all set up, we can read in our shape file honey.shp. We will process the data by extracting a single year and turning it into a multiline string. Finally, we will plot the shape file with no data on top of it to see what the map looks like.
# load honey shapefile
honey_sf <- read_sf("honey.shp")
# get the data for 2008
honey2008 <- honey_sf[honey_sf$year == 2008, ]
# create a MULTILINESTRING object
honey2008_multiline <- st_cast(honey2008, "MULTILINESTRING")
usa_1 <- ggplot(data = honey2008) +
geom_sf()
usa_1
Now, we can add data. We will fill each state in the shape file with the price per pound (prcprlb) of honey in 2008.
usa_2 <- ggplot(data = honey2008) +
geom_sf(aes(fill = prcprlb)) +
ggtitle(label = "The Honey crisis of 2008", subtitle = "Price per lb")
usa_2
In some instances you may want to overlay one map on top of another. The ggplot2 package supports this by allowing you to add multiple geom_sf() layers to a plot. As we only have one map currently, we can repeat the above example by plotting our data filled map, but then layer on a map of the US capitals marked in black dots:
state_capitals_sf = read_sf("capitals.shp")
usa_2c <- ggplot() +
geom_sf(data = honey2008,aes(fill=prcprlb)) +
geom_sf(data = state_capitals_sf,fill=NA) +
ggtitle(label = "The Honey crisis of 2008", subtitle = "Price per lb")
usa_2c
We can also zoom in to our favorite state. We will look at both the entire map and Washington state in particular, zooming in with the appropriate coordinate range.
usa_4 <- ggplot(data = honey2008) +
geom_sf(aes(fill = yldprcl))
usa_4 / usa_4 + coord_sf(xlim = c(-126, -115), ylim = c(45,50))
We can also play around with color schemes and aesthetics to make unique maps. Here is an example taken directly from the first additional resource linked in this document:
ggplot(data = honey2008_multiline) +
geom_sf(color = "#FF6B58", alpha = 0.1, size = 4) +
geom_sf(color = "#FF6B58", alpha = 0.1, size = 3) +
geom_sf(color = "#FF6B58", alpha = 0.2, size = 2) +
geom_sf(color = "#FF6B58", alpha = 0.2, size = 1) +
geom_sf(color = "#FF6B58", alpha = 1, size = 0.5) +
geom_sf(color = "#F8B660", alpha = 0.1, size = 6, data = state_capitals_sf) +
geom_sf(color = "#F8B660", alpha = 0.1, size = 5, data = state_capitals_sf) +
geom_sf(color = "#F8B660", alpha = 0.2, size = 4, data = state_capitals_sf) +
geom_sf(color = "#F8B660", alpha = 0.2, size = 3, data = state_capitals_sf) +
geom_sf(color = "#F8B660", alpha = 0.4, size = 2, data = state_capitals_sf) +
geom_sf(color = "#F8B660", alpha = 1, size = 1, data = state_capitals_sf) +
labs(subtitle="An aesthetic look",
title="US States and their Captials",
caption = "Excluding Alaska and Hawaii") +
guides(size = guide_legend(override.aes = list(colour = "#FA5F70FF"))) +
theme(
panel.grid.major = element_blank()
)
However, keep in mind the guidelines on colorblind-friendly colors and simplicity in design!
Exercise 1: Plot a map of the the price per lb in 2010 next to the map from 2008 found above
Exercise 2: Next, plot the product value (prodval) from 2010. Zoom in on your favorite state, and plot it next to Washington state.