Add Points to Choropleth Map in ggplot2

2019-02-18 08:14发布

问题:

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?

回答1:

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:



标签: r maps ggplot2