Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map county borders on to a ggmap

Tags:

r

ggmap

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.

enter image description here

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.

like image 887
cdeterman Avatar asked Sep 27 '22 11:09

cdeterman


1 Answers

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")
like image 87
lukeA Avatar answered Oct 04 '22 15:10

lukeA