我花了几天时间搜索这个网站和其他人寻找解决方案,但还没有找到它。如果我的解决方案有另一页,我错过了,我道歉。
我找到了this 但重新加载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