[英]Choropleth mapping issue in R
编辑:我已经意识到我的问题的根源。 我只有有关其拥有数据的县的计数信息,该信息少于我要绘制的区域中的县数。
有理由认为,代码的问题所在:
mapnames <- map("county",plot=FALSE)[4]$names
colorsmatched <- d$colorBuckets [na.omit(match(mapnames ,d$stcon))]
有没有人对如何从地图库生成与纽约州,新泽西州,宾夕法尼亚州和宾夕法尼亚州的县数相匹配的适当长度的矢量提出建议? 我想合并我拥有的计数数据,并在没有相关信息的县中添加零。
我正在尝试按照此处介绍的教程进行操作: http : //www.thisisthegreenroom.com/2009/choropleths-in-r/
下面的代码执行了,但是它不能正确匹配我的数据集和maps_counties数据,或者没有按照我期望的顺序进行绘制。 例如,更大的纽约市面积的结果区域没有显示密度,而宾夕法尼亚州的随机县显示了最高密度。
我的数据表的一般格式为:
county state count
fairfield connecticut 17
hartford connecticut 6
litchfield connecticut 3
new haven connecticut 12
...
...
westchester new york 70
yates new york 1
luzerne pennsylvania 1
请注意,此数据按州和县顺序排列,包括CT,NJ,NY和PA的数据。
首先,我读入数据集:
library(maps)
library(RColorBrewer)
d <- read.table("gissum.txt", sep="\t", header=TRUE)
#Concatenate state and county info to match maps library
d$stcon <- paste(d$state, d$county, sep=",")
#Color bins
colors = brewer.pal(5, "PuBu")
d$colorBuckets <- as.factor(as.numeric(cut(d$count,c(0,10,20,30,40,50,300))))
这是我的搭配
mapnames <- map("county",plot=FALSE)[4]$names
colorsmatched <- d$colorBuckets [na.omit(match(mapnames ,d$stcon))]
绘图:
map("county"
,c("new york","new jersey", "connecticut", "pennsylvania")
,col = colors[d$colorBuckets[na.omit(match(mapnames ,d$stcon))]]
,fill = TRUE
,resolution = 0
,lty = 0
,lwd= 0.5
)
map("state"
,c("new york","new jersey", "connecticut", "pennsylvania")
,col = "black"
,fill=FALSE
,add=TRUE
,lty=1
,lwd=2
)
map("county"
,c("new york","new jersey", "connecticut", "pennsylvania")
,col = "black"
,fill=FALSE
,add=TRUE
, lty=1
, lwd=.5
)
title(main="Respondent Home ZIP Codes by County")
我确信我缺少一些基本的东西:maps函数绘制项目的顺序-但我似乎无法弄清楚。 谢谢您的帮助。 如果您需要更多信息,请告诉我。
通过将数据与选择状态映射中的数据合并,可以解决您的问题。 这是您要找的东西吗?
library(maps);
library(RColorBrewer);
# Create Dummy Data Frame to Play With
d = rbind(c('fairfield','connecticut',17),c('westchester','new york',70), c('luzerne','pennsylvania',1));
d = data.frame(d);
names(d) = c("county", "state", "count");
d$count = as.numeric(as.character(d$count));
d$stcon = paste(d$state, d$county, sep=",");
# Extract mapnames for States
mapnames2 = map("county",c("new york","new jersey", "connecticut", "pennsylvania"),plot=FALSE)[4]$names;
mapnames2 = data.frame(mapnames2);
names(mapnames2) = "stcon";
# Merge with d
d = merge(mapnames2, d, all = T);
d$count[is.na(d$count)] = 0;
# Color bins
colors = brewer.pal(5, "PuBu");
d$colorBuckets = as.factor(as.numeric(cut(d$count,c(0,10,20,30,40,50,300))));
map("county"
,c("new york","new jersey", "connecticut", "pennsylvania")
,col = colors[d$colorBuckets]
,fill = TRUE
,resolution = 0
,lty = 0
,lwd= 0.5
)
map("state"
,c("new york","new jersey", "connecticut", "pennsylvania")
,col = "black"
,fill=FALSE
,add=TRUE
,lty=1
,lwd=2
)
map("county"
,c("new york","new jersey", "connecticut", "pennsylvania")
,col = "black"
,fill=FALSE
,add=TRUE
, lty=1
, lwd=.5
)
title(main="Respondent Home ZIP Codes by County")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.