简体   繁体   中英

Making a state map highlighting a specific county and point in R (ggmaps, maps, ggplot2)

I am trying to build a map of state, that has its counties outlined, and one county colored in blue with a point for a specific resort. Alas, I am having trouble getting a county colored or adding a specific point. My code builds off of http://eriqande.github.io/rep-res-web/lectures/making-maps-with-R.html Thank you for any insights!

library(ggplot2)
library(ggmap)
library(maps)
library(mapdata)    

states <- map_data("state")
dim(states)
ut_df <- subset(states, region == "utah")
head(ut_df)

counties <- map_data("county")
ut_county <- subset(counties, region == "utah")
head(ut_county)

ut_base <- ggplot(data = ut_df, mapping = aes(x = long, y = lat, group = 
group)) +
coord_fixed(1.3) +
geom_polygon(color = "black", fill = "gray")

ut_base + theme_nothing() +
geom_polygon(data = ut_county, fill = NA, color = "white") +
geom_polygon(color = "black", fill = NA)  # get the state border back on top
# Select a subregion
single_county <- subset(ut_county, subregion=="utah")

# Fill the selected subregion with a predefined color and
# plot a colored point with a specified long. and lat.
ut_base + theme_void() +
geom_polygon(data = ut_county, fill = NA, color = "white") +
geom_polygon(color = "black", fill = NA) +
geom_polygon(data = single_county, fill = "red", color = "white") +
geom_point(x=-111.8, y=40.2, col="blue", size=3)

在此处输入图片说明

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM