ggplot填充SpatialPolygonsDataFrame

时间:2018-11-28 19:53:29

标签: r ggplot2

我正在尝试绘制美国县SPDF并绘制出现在另一个向量中的县的阴影(按GEOID进行匹配)。这是我目前拥有的代码,但是所有县都没有阴影。

我已从此link下载了美国县边界的ShapeFile。

library(ggplot2)
library(raster)
US_counties <- shapefile("~/us_county_shp_files/tl_2017_us_county.shp")
counties_in_radius <- c("12011", "12015", "12021", "12027", "12043", "12049", "12051", "12055", "12061")
base_plot <- ggplot(data = US_counties) +
  coord_fixed(ratio = 1.3) +
  geom_polygon(data=US_counties, aes(x = long, y = lat, group = group), size = 0.1, colour = "gray", 
               fill= (if(US_counties$GEOID %in% counties_in_radius) "blue" else NA))
base_plot

如果我只是执行GEOID,我将得到未找到错误对象'GEOID',而当我执行US_counties $ GEOID时,将得到“条件的长度> 1,并且将仅使用第一个元素”。

如何正确填充counties_in_radius中的县?

0 个答案:

没有答案