R的美国Choropleth地图

时间:2014-03-27 06:32:50

标签: r maps

我几乎阅读了每个谷歌链接,试图制作其中一个。

我正在使用gtd数据集(http://www.start.umd.edu/gtd/contact/

我要做的是根据使用地图套件在该州发生的攻击次数,为美国地图的状态着色。

根据我见过的代码,我知道我需要将数据中的名称与地图包中的名称相匹配,然后选择要绘制的数据。

US<- subset(x=gtd, country_txt="United States")
stateattacks<- ddply(US, .(provstate), "nrow")
data(state) 
mapnames<- map("state", plot=FALSE)$names
region_list<- strsplit(mapnames, ":")
mapnames2<- sapply(region_list, "[", 1)
m<- match(mapnames2, tolower(stateattacks$provstate)
map.area <- nrow[m] #nrow is the variable I am trying to match.. but this step gives Error in nrow[m] : object of type 'closure' is not subsettable. 

我只是试图根据该状态(nrow)中的攻击次数来映射每种状态颜色,但我无法对其进行配置。

1 个答案:

答案 0 :(得分:1)

回到旧时代&#34;这些合唱团在R中做了一些工作但是在Trulia的优秀人员的choroplethr包装中,它比在R中更容易(至少对于美国的等值线):

library(choroplethr)

gtd <- read.csv("gtd.csv")

US <- gtd[gtd$country_txt == "United States",]

stateattacks<- ddply(US, .(provstate), "nrow")

# choroplethr needs these column names
colnames(stateattacks) <- c("region", "value")

# choroplethr might do this internally (have not checked)
# but it's not a bad thing to do anyway
stateattacks$region <- tolower(stateattacks$region)

# it won't work with the (…) bit and that might have been your 
# problem with "old school' chorpleths not working
stateattacks$region <- gsub(" (u.s. state)", "", stateattacks$region, fixed=TRUE)

# make the US choropleth
choroplethr(stateattacks, lod="state")

enter image description here

包装中有很多自定义选项。

对于这种特殊情况的一个等级可能很好,但对于其他类型的数据,考虑对人口进行规范化通常是一个好主意,否则它可能会错误地扭曲消息。

相关问题