Choropleth邮政编码

Choropleth邮政编码,r,zipcode,choropleth,R,Zipcode,Choropleth,我试图在弗吉尼亚州的部分地区创建一个邮政编码列表,以显示一些公司数据。除了最后一行aes(fill=growth),我可以让所有内容正常运行。在这里,我得到一个错误: 错误:美学长度必须为1,或与数据长度相同。问题:增长 以下是我的数据: 我感兴趣的zipcodes的名称和相应的公司数据值 我的代码: library(ggplot2) library(maptools) library(rgdal) library(plyr) #set working directory setwd("

我试图在弗吉尼亚州的部分地区创建一个邮政编码列表,以显示一些公司数据。除了最后一行
aes(fill=growth)
,我可以让所有内容正常运行。在这里,我得到一个错误:

错误:美学长度必须为1,或与数据长度相同。问题:增长

以下是我的数据:

  • 我感兴趣的zipcodes的名称和相应的公司数据值
我的代码:

library(ggplot2)
library(maptools)
library(rgdal)
library(plyr)

#set working directory
setwd("F:/Dropbox/Zip Codes")

#load Shapefile NOVA
Zips <- readOGR(dsn="F:/Dropbox/Zip Codes", layer="NOVA")

#load Company Data of Zip Codes
Company <- read.csv("Data.csv")

#set to data.frame
Company_df <- data.frame(Company)

#create growth vector
growth = Company_df[,'Growth']

#merge growth vector into Zips
Zips$growth = growth

#ggplot
Nmap = ggplot(Zips) +
aes(x = long, y = lat, group=group) +
geom_polygon() +
aes(fill = growth)
Nmap
库(ggplot2)
图书馆(地图工具)
图书馆(rgdal)
图书馆(plyr)
#设置工作目录
setwd(“F:/Dropbox/邮政编码”)
#加载形状文件NOVA

Zips我对你的目录结构的组织有点不同。尽管Internet上有较旧的代码片段,但您不需要将数据绑定到数据帧。但是,您确实需要增强多边形以与ggplot一起使用。另外,
read.csv
生成一个data.frame,因此您不需要从该调用中重新生成一个

library(ggplot2)
library(maptools)
library(rgdal)
library(ggthemes)

setwd("~/Development/nova_choro")

zips <- readOGR(dsn="zip_codes/NOVA.shp", layer="NOVA")
company <- read.csv("data.csv")

# this makes the polygons usable to ggplot2
# and by using ZCTA5CE10 as the region id, 
# you can then use that equivalent id field 
# from the actual company data frame for the
# choropleth colors

zips_map <- fortify(zips, region="ZCTA5CE10")

gg <- ggplot()
# draw the base map polygons
gg <- gg + geom_map(data=zips_map, map=zips_map,
                    aes(x=long, y=lat, map_id=id),
                    color="#7f7f7f", fill="white", size=0.25)
# fill in the polygons
gg <- gg + geom_map(data=company, map=zips_map,
                    aes(fill=Growth, map_id=zip_code_area),
                    color="#7f7f7f", size=0.25)
# better color scheme
gg <- gg + scale_fill_distiller(palette="Greens")
# no "slashes" in the legend boxes
gg <- gg + guides(fill=guide_legend(override.aes=list(colour=NA)))
# use an actual projection (there may be a better one for NOVA
gg <- gg + coord_map()
# get rid of map chart junk
gg <- gg + theme_map()
gg
库(ggplot2)
图书馆(地图工具)
图书馆(rgdal)
图书馆(主题)
setwd(“~/Development/nova_choro”)

拉链清晰,非常有用。非常感谢。