Get boundaries to come through on states
Asked Answered
A

1

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

Admeasurement answered 15/1, 2015 at 5:36 Comment(12)
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?Selfconscious
@Selfconscious 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.Admeasurement
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.Selfconscious
@Selfconscious 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 = TRUEAdmeasurement
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 inSelfconscious
@Selfconscious Thank you....it's getting there....Now I just need the state borders to come through....any idea? I've updated the mapAdmeasurement
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.Subprincipal
@RomanLuštrik Thanks for your suggestions. I updated it and am getting the lines, but not data coming through. Any further suggestions?Admeasurement
Can you make your example reproducible? We don't have access to PRISM_1895_test.Subprincipal
@RomanLuštrik It's a fairly large dataset so I'll dput a portion of it when I get home today. Thanks for your helpAdmeasurement
@RomanLuštrik I've updated with an rep example....thanks for any helpAdmeasurement
@Selfconscious I've updated with a rep example if you can help further :) ThanksAdmeasurement
S
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

Selfconscious answered 16/1, 2015 at 2:6 Comment(1)
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 APPTAdmeasurement

© 2022 - 2024 — McMap. All rights reserved.