在ggplot2中为Choropleth地图添加点 [英] Add Points to Choropleth Map in ggplot2

查看:222
本文介绍了在ggplot2中为Choropleth地图添加点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在看哈德利韦克汉姆的 ggplot2 的例子:

 <$ c $ b $ library(ggplot2)
library(maps)
states< -map_data(state)
arrests< -USArrests
名称(逮捕)< -tolower(名称(逮捕))
逮捕$地区<-tolower(rownames(USArrests))

氯< - 合并(州,逮捕,= =地区)
氯<氯[order(chloro $ order),]
qplot(long,lat,data = chloro,group = group,fill = assault,geom =polygon)

然后我想为一些着名的美国城市添加点数到地图上,但我一直无法。我试过了:

pre $ base $ map $ lt; -qplot(long,lat,data = chloro,group = group,fill = assault, geom =polygon)
base_map + qplot(long,lat,data = us.cities)+ border(state,size = .5)

但是我得到以下错误:

  p + o:非二进制运算符的非数字参数
另外:警告消息:
+不兼容的方法(+ .ggplot,Ops.data.frame)

如何添加这些点数?

解决方案

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

虽然您可能想要排除HI和AK中的城市,但我也是这样做的:


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?

解决方案

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:

这篇关于在ggplot2中为Choropleth地图添加点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆