我正在尝试创建具有县和州边界的 ggplot 地图。我可以生成带有县界的地图,但是当添加以下行以获取州边界时,我遇到了问题。该代码复制如下。
ggplot2::geom_polygon(aes(x= long, y = lat, group = group), fill = NA, color = "black",
data = filter(us_map(), abbr %in% states_of_interest))
运行此代码时,出现错误:
“ FUN(X [[i]],...)中的错误
未找到对象'long'”。我相对不熟悉us_map程序包,但有人告诉我在lat上为x和y使用long可以正常工作。
我尝试添加命令
“ inherit.aes = FALSE”
根据其他堆栈溢出后的建议,但并不能解决错误。
答案 0 :(得分:0)
us_map
函数返回一个带有经纬度的x
和y
的对象。您可以分别存储县和州,然后将它们都作为多边形添加到ggplot
中。
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()