我ggplot2
用来创建人口密度等值线。它目前适用于单个州,但不适用于多个州。似乎各个县(通常具有相同名称)的密度混淆了,有时甚至不匹配的县也会在州之间混淆。例如,“New Jersey”给出了正确的密度,但“New Jersey”、“New York”告诉我新泽西州人口众多的埃塞克斯县的密度 <30p/mi^2。为什么是这样?
library(stringr)
library(ggplot2)
library(scales)
library(maps)
popdensitymap <- function(...){
path <- "U:/maps-county2011.csv"
states <- list(...)
countydata <- read.csv(path, sep=",")
countydata <- data.frame(countydata$X, countydata$Population.Density)
names(countydata) <- c("fips", "density")
data(county.fips)
cdata <- countydata
cdata$fips <- gsub("^0", "", cdata$fips)
countyinfo <- merge(cdata, county.fips, by.x="fips", by.y="fips")
countyinfo <- data.frame(countyinfo, str_split_fixed(countyinfo$polyname, ",", 2))
names(countyinfo) <- c('fips', 'density', 'polyname', 'state', 'county')
countyshapes <- map_data("county", states)
countyshapes <- merge(countyshapes, countyinfo, by.x="subregion", by.y="county")
choropleth <- countyshapes
choropleth <- choropleth[order(choropleth$order), ]
choropleth$density_d <- cut(choropleth$density, breaks=c(0,30,100,300,500,1000,3000,5000,100000))
state_df <- map_data("state", states)
density_d <- choropleth$density_d
choropleth <- choropleth[choropleth$state %in% tolower(states),]
p <- ggplot(choropleth, aes(long, lat, group=group))
p <- p + geom_polygon(aes(fill=density_d), colour=alpha("white", 1/2), size=0.2)
p <- p + geom_polygon(data = state_df, colour="black", fill = NA)
p <- p + scale_fill_brewer(palette="PuRd")
p
}
要使用,
popdensitymap("New Jersey")
popdensitymap("New York", "New Jersey")
这是csv。它非常难看,但我现在无法访问文件共享系统。
这是输出的示例。如您所见,纽约市人口众多的埃塞克斯县的代表不准确。
编辑:这是我的 CSV 版本。抱歉,保管箱延迟。