Using usmap
, I have made a plot of values by US counties from some values stored in a variable county
. Before I recently updated R and all my packages, I could easily create a county map with thick state borders and with state abbreviations using the following code:
library(ggplot2)
library(usmap)
centroid_labels = usmapdata::centroid_labels("states")
plotdf = data.frame(cbind(county$FIPS,county$SES)); plotdf[,2] = as.numeric(as.character(plotdf[,2])); names(plotdf) = c("fips", "SES");
plot_usmap(data = plotdf, values = "SES", color = NA) + # color = NA removes county boundary lines
geom_polygon(data = usmapdata::us_map(regions = "states"), aes(x, y, group = group), fill = NA, linewidth = 1, color = "black") + # Keep state boundary lines
geom_text(data = centroid_labels, aes(x = x, y = y, label = abbr), fontface = "bold", colour = "gray12", size = 3)
With a few more lines to change colors and such, that would produce a picture like this:
However, as mentioned, since I updated packages, this doesn't work for me anymore. Specifically, it gives me and error that the object x
wasn't found. Searching for an answer, it seems like others have stumbled into a similar issue. This means that I can draw the counties, but the code lines that creates state lines and state abbreviations no longer work. Removing what doesn't work, it will produce something like this:
Question: Is there any other way to draw state boundaries in this county map and also add state abbreviations?
Here is another approach that is closer to the original question asked.
library(ggplot2)
library(usmap)
# Extract data for states and counties
states_data <- us_map("states")
counties_data <- us_map("counties")
counties_data$SES <- rnorm(3144, 100, 100)
centroid_labels = usmapdata::centroid_labels("states")
# Map
plot_usmap("counties", color = 'NA') +
geom_sf(data = counties_data,
mapping = aes(geometry = geom, fill = SES),
color = NA) +
geom_sf(data = states_data,
mapping = aes(geometry = geom),
color = 'black',
linewidth = 1,
fill = NA) +
geom_sf_text(data = centroid_labels,
mapping = aes(geometry = geom, label = abbr),
fontface = 'bold',
color = 'gray12',
size = 3) +
scale_fill_gradient2(low = "blue", high = "red", mid = 'white',
midpoint = 0) +
theme(legend.position = 'none')
Created on 2024-05-24 with reprex v2.1.0