我正在尝试ggplot2
使用来自不同数据帧的数据来构建地图。
library(maptools)
xx <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1], IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))
xx.sub1 <- subset(xx, xx$FIPSNO < 37010)
xx.sub2 <- subset(xx, xx$FIPSNO > 37010)
xx.sub1@data$id <- rownames(xx.sub1@data)
xx.sub1.points <- fortify(xx.sub1, region="id")
xx.sub1.df = plyr::join(xx.sub1.points, xx.sub1@data, by="id")
xx.sub2@data$id <- rownames(xx.sub2@data)
xx.sub2.points <- fortify(xx.sub2, region="id")
xx.sub2.df = plyr::join(xx.sub2.points, xx.sub2@data, by="id")
ggplot(xx.sub2.df) +
aes(long, lat, fill = (SID79/BIR79)*1000, group = group) +
geom_polygon() + geom_path(color="grey80") +
coord_equal() +
scale_fill_gradientn(colours = RColorBrewer::brewer.pal(7, "YlOrBr")) +
geom_polygon(data = xx.sub1.df, fill = "grey50") +
geom_path(data = xx.sub1.df, color="grey80") +
labs(fill = "Mapped value", title = "Title")
到目前为止,一切都按预期工作,我得到了一张漂亮的地图:
但是,我想更改的是为数据添加单独的图例xx.sub1.df
- 因为所有多边形都只是用灰色填充,我希望这将是一个额外的条目。
我怎样才能做到这一点?