0

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

Vedda
  • 7,066
  • 6
  • 42
  • 77
  • I've never seen a google map that's been clipped to a state boundary. It usually always show the state/region in context. Are you sure you want to be using `qmap` for this? Is there an existing plot you are trying to imitate? – MrFlick Jan 15 '15 at 05:53
  • @MrFlick I've updated the post to reflect a map I'm trying to replicate. It's not ggmap, but it's what I'm trying to do. – Vedda Jan 15 '15 at 05:55
  • Your desired map is closer to this strategy: `library(maps); library(ggplot2); regions<- c("montana","wyoming","colorado","new mexico","north dakota","south dakota","nebraska","kansas","oklahoma","texas","minnesota","iowa","missouri","arkansas"); ggplot()+geom_polygon(data=subset(map_data("county"), region %in% regions), aes(x=long, y=lat, group=group, fill=region))` since it looks like you want county data. – MrFlick Jan 15 '15 at 06:10
  • @MrFlick Thanks for your help. I've updated the question with how far I've gotten. I just need the borders now, but don't know how to make the switch because fill = TRUE – Vedda Jan 15 '15 at 06:45
  • Did you look at the `db_map` object? Using those commands there's no "group" column generated. If you just change my example to `geom_polygon(data=subset(map_data("county"), region %in% regions), aes(x=long, y=lat, group=group, fill=region), col="black")` will draw the borders in – MrFlick Jan 15 '15 at 06:50
  • @MrFlick Thank you....it's getting there....Now I just need the state borders to come through....any idea? I've updated the map – Vedda Jan 15 '15 at 07:00
  • Try switching the order of `geom_points` and `geom_polygon`. I suggest you do everything in one ggplot call and have each geom in a separate line. In my opinion, this improves readability. – Roman Luštrik Jan 15 '15 at 10:40
  • @RomanLuštrik Thanks for your suggestions. I updated it and am getting the lines, but not data coming through. Any further suggestions? – Vedda Jan 15 '15 at 17:02
  • Can you make your example reproducible? We don't have access to `PRISM_1895_test`. – Roman Luštrik Jan 15 '15 at 18:18
  • @RomanLuštrik It's a fairly large dataset so I'll dput a portion of it when I get home today. Thanks for your help – Vedda Jan 15 '15 at 18:54
  • @RomanLuštrik I've updated with an rep example....thanks for any help – Vedda Jan 16 '15 at 00:45
  • @MrFlick I've updated with a rep example if you can help further :) Thanks – Vedda Jan 16 '15 at 00:47

1 Answers1

2

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

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Great thank you so much.... This is what I needed....I really appreciate all your help...is there anything else you would do to this graph to make it look any better? In particular to the shading and differences in APPT – Vedda Jan 16 '15 at 02:15