Map county borders on to a ggmap

…衆ロ難τιáo~ 提交于 2019-12-06 03:31:15

问题


I am generating a few maps and I would like show the borders of counties on top of a ggmap roadmap. Here is an example using part of Texas.

library(ggmap)
map = get_map(location = c(-95.31619, 28.42460), 
              zoom = 6, source = "google", maptype="roadmap")
map.plot = ggmap(map)

# get texas counties
counties <- map_data("county")
tx_county <- subset(counties, region == 'texas')

map.plot + 
  theme_nothing() + 
  geom_polygon(data = tx_county, aes(x=long, y=lat), fill = NA, color = "red")

However, the resulting figure has lines crossing counties instead of just the borders.

Any thoughts on what I am doing wrong? I have seen another example here where it works when only using ggplot2 but I would like to use the 'roadmap' from ggmap.


回答1:


You need to set the grouping for the polygons:

map.plot + 
  geom_polygon(data = tx_county, aes(x=long, y=lat, group = group), fill = NA, color = "red")


来源:https://stackoverflow.com/questions/33155672/map-county-borders-on-to-a-ggmap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!