我花了几天时间在这个网站和其他网站上搜索解决方案,但还没有找到。如果我的解决方案还有另一页,而我错过了,我深表歉意。
我发现了这一点 ,但重新加载 ggplot2 和 rgdal(分离后)并没有解决它。
我正在使用 ZCTA(邮政编码制表区)的人口统计数据来覆盖 Google 地形图上的多边形。我能够使用 qmap 正确绘制多边形,但是在我合并人口统计数据后,这些图都是错误的。我试过指定顺序,并使用合并。(哎呀,我已经尝试了各种各样的事情。)我很想得到一些帮助。
是一个工作图,在合并之前,并且
是之后。
这是我的代码:
# shapefile from Census
fips34 <-readOGR(".", "zt34_d00", stringsAsFactors = FALSE)
# zip code areas, 1 row per ZCTA with nonmissing Census data
ptInd <-read.dta("ptIndzcta.dta")
keepzips <- fips34
keepzips@data$id <-rownames(keepzips@data) # create idvar to remerge
keepzipsdat <- fortify(keepzips, region="id") # fortify
keepzipsdat <- keepzipsdat[order(keepzipsdat$order),] # clarify order
keepzipsdat <- join(keepzipsdat, keepzips@data, by="id") # remerge for zcta
qmap("new jersey", zoom = 8, maptype="terrain", color="bw") +
geom_polygon(aes(x=long, y=lat, group=group),
data=keepzipsdat) + coord_equal() # this map plots fine
# now merge in data to create choropleth
zip2 <- merge(keepzipsdat, ptInd, by.y="zcta5", by.x="ZCTA", all.x = TRUE)
zip2[order(zip2$order),] # reestablish order, is this necessary?
qmap("new jersey", zoom = 8, maptype="terrain", color="bw") +
geom_polygon(aes(x=long, y=lat, group=group),
data=zip2) + coord_equal() # this looks crazy
ggplot(data=zip2, aes(x=long, y=lat, group=group)) + geom_polygon()
# also crazy
# and this is before assigning a fill variable to the polygons