Map border bordered by ggmap

I am creating several maps, and I would like to show the boundaries of the counties on top of the roadmap ggmap. Here is an example of using parts 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, not just borders.

enter image description here

Any thoughts on what I'm doing wrong? I saw another example here where it works when used only ggplot2, but I would like to use the roadmap from ggmap.

+4
source share
1 answer

You need to set grouping for polygons:

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

Source: https://habr.com/ru/post/1611832/


All Articles