简体   繁体   English

制作突出显示R中特定县和点的州图(ggmaps,maps,ggplot2)

[英]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. las,我在给县上色或添加特定点时遇到麻烦。 My code builds off of http://eriqande.github.io/rep-res-web/lectures/making-maps-with-R.html Thank you for any insights! 我的代码基于http://eriqande.github.io/rep-res-web/lectures/making-maps-with-R.html构建。感谢您的见解!

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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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