Search code examples
rggplot2ggmapqmap

Get boundaries to come through on states


Edit 7 :

After quite a bit of help, I've been able to get a map that is getting close to the results I need. But I still need to have the state boundaries come through on the map, but I can't figure it out. In order to make a reproducible example that would be appropriate I need to link to the data set since the dput is so large.

To make things easy, I subset only three states, but where the boundary lines do not show up. I would like to be able to have the boundary lines come through the plot as white lines, like they are on the rest of the map. Thanks for your help.

Dataset :

https://www.dropbox.com/s/0evuvrlm49ab9up/PRISM_1895_db.csv?dl=0

Rep Code :

PRISM_1895_db <- read.csv("PRISM_1895_db.csv")

regions<- c("north dakota","south dakota","nebraska","kansas","oklahoma","texas","minnesota","iowa","missouri","arkansas", "illinois", "indiana", "wisconsin")

ggplot() + 
  geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group), col="white") +
  geom_point(data = PRISM_1895_db2, aes(x = longitude, y = latitude, color = APPT), alpha = .5, size = 3.5) 

Graph :

enter image description here


Solution

  • The order in which you draw the layers matters. If you want the while lines on top, you'll need to add them last. And if you want the black shapes in the background, you need them first. So basically you need to split up the states into two draws: the background and the outline.

    ggplot() + 
      geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group)) +
      geom_point(data = PRISM_1895_db, aes(x = longitude, y = latitude, color = APPT), alpha = .5, size = 3.5) +
      geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group), color="white", fill=NA)
    

    which produces

    enter image description here