我对 R 很陌生,并且一直在尝试使用 ggplot2 来尝试复制县级失业数据,使用 FIPS 代码作为 shapefile 和数据之间的识别变量。几年前,我尝试对 Hadley 和 Barry 使用的方法进行网格划分(http://blog.revolutionanalytics.com/2009/11/choropleth-challenge-result.html),但我一直遇到错误:Aesthetics must either be length one, or the same length as the dataProblems:freq_a
几天来,我一直试图找出如何解决这个问题,所以非常感谢任何帮助。
这是我的代码:
require(rgdal)
require(maps)
require(rgdal)
require(RColorBrewer)
require(ggplot2)
unem<-read.table("unemployment09.csv",sep=",",
as.is=TRUE,
colClass=c(rep("character",8),"numeric"),
col.names=c("code","f1","f2","name","yr","x1","x2","x3","percentage")
)
unem$fips=paste(unem$f1,unem$f2,sep="")
county<-readOGR("tl_2009_us_county.shp",
"tl_2009_us_county")
colClasses=c("character", "character")
county$fips<-paste(county$STATEFP,county$COUNTYFP,sep="")
m<-match(county$fips, unem$fips)
county$freq<-unem$percentage[m]
county$freq[is.na(county$freq)]=0
county$freq_a<-cut(county$freq, breaks = c(seq(0,10,by=2),35))
county$freq_a<-as.numeric(county$freq_a)
ggplot(county, aes(long, lat, group = group)) +
geom_polygon(aes(fill = county$freq_a), colour = "white", size = 0.2) +
scale_fill_brewer(palette = "PuRd") + theme(panel.background = element_rect(fill =
"white"))
谢谢