Omit some borders in ggplot/ggmap
Asked Answered
A

1

8

I'm very new to R and I was just playing around with a project to plot projected population growth of Alabama counties from 2010 to 2020. Here's my code:

dat <- read.table("C:/Users/rasmus/Documents/countyinfo.txt", sep="\t", header=TRUE)

library(ggplot2)
library(maps)
library(ggmap)

mdat <- map_data('county')
str(mdat)
al1 = get_map(location = c(lon = -86.304474, lat = 32.362563), 
              zoom = 7, maptype = 'roadmap')
al1MAP = ggmap(al1) + 
         geom_point(data=dat,inherit.aes = FALSE, 
                    aes(y=Lat, x=Lon, map_id=County, size=Growth), col="red") + 
         borders("state", colour="red", alpha=0.8) + 
         borders("county", colour="blue", alpha=0.5)
al1MAP

Now, I have two questions.

1) The state borders seem to be doing weird things. Here's a screenshot with the county overlay turned off:

enter image description here

2) Given that this is only about Alabama, I would like to turn borders outside the state borders off, but I don't know how to do that. My guess would be to experiment with xlim and ylim, but I don't know how to restrict those to the Alabama border polygon.

Anglesey answered 28/3, 2013 at 8:31 Comment(0)
D
12

It seems that with function borders() for coordinates of some states are connected together.

To solve this problem you can store state borders as separate data frame using map_data() and then add state borders using geom_path() to your map. In geom_path() add group=region to ensure that points are connected only according one region.

To show borders just for the Alabama counties you should add argument region="alabama" to function borders().

al1 = get_map(location = c(lon = -86.304474, lat = 32.362563), 
      zoom = 6, maptype = 'roadmap')
mdat <- map_data('state')
ggmap(al1) + 
  geom_path(data=mdat,aes(x=long,y=lat,group=region),colour="red",alpha=0.8)+
  borders("county", colour="blue", alpha=0.5,region="alabama")

enter image description here

Dewittdewlap answered 28/3, 2013 at 8:41 Comment(5)
Thanks! This works excellently. I don't know if it's etiquette here to ask this as a different question, but I'm just going to ask here-- is there any way how I can separate the points into two different colors based on a cutoff? (ie if Growth>0 then "blue" else "red").Anglesey
I've been fooling around with scale_color_manual, but I'm not getting it to work. The two things I've tried mainly were defining a function as if x>0, "red", "blue" and then calling it, but then I get something like "Error in ifelse(Growth < 0, "red", "blue") : object 'Growth' not found" I've also tried adding variations on "aes(colour =Growth>0) + scale_colour_manual(values=c("red","blue")))", but then it rightly complains about a "non-numeric argument to binary operator". I feel kind of bad about asking questions this basic, but I'm really new to the program.Anglesey
@LuciusSergiusCatilina Try geom_point(data=dat,inherit.aes = FALSE, aes(y=Lat, x=Lon, size=Growth, color=Growth>0))+ scale_colour_manual(values=c("red","blue"))Dewittdewlap
thanks, that worked just fine. I also added "+ theme(legend.position = "none") ". I really like the map now!Anglesey
Gah! Where the **** is inherit.aes documented when looking through the docs for geom_XX?Gitagitel

© 2022 - 2024 — McMap. All rights reserved.