Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Points to Choropleth Map in ggplot2

Tags:

r

maps

ggplot2

I'm looking at the following example from Hadley Wickham's ggplot2:

library(ggplot2)
library(maps)
states<-map_data("state")
arrests<-USArrests
names(arrests)<-tolower(names(arrests))
arrests$region<-tolower(rownames(USArrests))

chloro<-merge(states, arrests, by="region")
chloro<-chloro[order(chloro$order), ]
qplot(long, lat, data=chloro, group=group, fill = assault, geom="polygon")

I would then like to add points for some notable US cities to the map, but I haven't been able to. I've tried:

base_map<-qplot(long, lat, data=chloro, group=group, fill = assault, geom="polygon")
base_map + qplot(long, lat, data=us.cities) + borders("state", size=.5)

But I get the following error:

Error in p + o : non-numeric argument to binary operator
In addition: Warning message:
Incompatible methods ("+.ggplot", "Ops.data.frame") for "+""

How can I add these points?

like image 632
Burton Guster Avatar asked Sep 09 '12 17:09

Burton Guster


1 Answers

This seems to work just fine for me:

base_map + 
    geom_point(aes(long, lat,fill = NULL,group = NULL), size = 1,data=us.cities) + 
    borders("state", size=.5)

Although you may want to exclude the cities in HI and AK, as I did to produce this version of the plot:

enter image description here

like image 80
joran Avatar answered Sep 24 '22 02:09

joran