我使用的是gtd数据集(http://www.start.umd.edu/gtd/contact/)
我正在尝试做的是使用maps包,根据在该州发生的攻击次数,对美国地图的州进行着色。
根据我看到的代码,我知道我需要将我的数据中的名称与地图包中的名称进行匹配,然后根据这些数据选择要打印的数据。
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)来映射每个状态的颜色,但我无法对其进行配置。
发布于 2014-03-27 18:46:24
回到“旧时代”,这些choropleths需要在R中做一些工作,但使用Trulia的优秀人员提供的choroplethr包,这比在R中要容易得多(至少对于美国的choropleths来说是这样):
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")

软件包中有大量的自定义选项。
对于这种特定情况的一致意见可能是好的,但对于其他类型的数据,考虑对总体进行标准化通常是一个好主意,否则可能会不适当地扭曲消息。
https://stackoverflow.com/questions/22679951
复制相似问题