I am attempting to create a ggplot map with both county and state boundaries. I am able to produce the map with county boundaries, but when adding the following line to get state borders as well, I run into an issue. The code is reproduced below.
ggplot2::geom_polygon(aes(x= long, y = lat, group = group), fill = NA, color = "black",
data = filter(us_map(), abbr %in% states_of_interest))
When I run this code, I get the error:
"Error in FUN(X[[i]], ...)
object 'long' not found". I am relatively unfamiliar with the us_map package, but I was told using long at lat for x and y would function correctly.
I have tried adding the command
"inherit.aes = FALSE"
upon other stack overflow post recommendations, but it did not solve the error.
The us_map
function returns an object with x
and y
for lat/long. You can store the counties and states separately, then add them both to ggplot
as polygons.
library(usmap)
states_of_interest <- c("CA","OR", "WA")
counties <- us_map(regions = "counties", include = states_of_interest)
states <- us_map(include = states_of_interest)
ggplot() +
geom_polygon(data = counties, aes(x = x, y = y, group = group), fill = NA, color = "black") +
geom_polygon(data = states, aes(x = x, y = y, group = group), fill = NA, color = "red") +
coord_equal()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.