In this video I use the ggplot2 package and the built in USArrests data in R to create a color coded plot of the United States of America. I explain what the various bits of code do and highlight some of the main issues that people run into when trying to produce a color coded map.
Subscribe below to get updates on my latest videos, courses, and other useful information.
library(maps)
library(mapproj)
library(tidyverse)
# get state data and arrests data
states <- map_data("state")
arrests <- USArrests
# adjust case for matching
names(arrests) <- tolower(names(arrests))
arrests$region <- tolower(rownames(USArrests))
# merge and sort (plots in order, sort ensures states filled in)
arrests.geo <- merge(states, arrests, sort = FALSE, by = "region")
arrests.geo <- arrests.geo[order(arrests.geo$order), ]
# plot
ggplot(arrests.geo, aes(long, lat)) +
geom_polygon(aes(group = group, fill = assault)) +
coord_map()