我正在尝试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") …Run Code Online (Sandbox Code Playgroud) 我想要:
期望的输出将是
到目前为止我失败的尝试.预先感谢您的任何帮助!
library(ggplot2)
set.seed(1337)
dat <- structure(list(id = structure(c(2L, 2L, 2L, 2L),
.Label = c("1.1", "1.2", "1.3", "2.1", "2.2", "2.3"),
class = "factor"),
value = c(3.1, 3.1, 3.1, 3.1),
x = c(2.2, 1.1, 1.2, 2.5),
y = c(0.5, 1, 2.1, 1.7)),
class = "data.frame",
row.names = c(NA, -4L))
line <- data.frame(
x = cumsum(runif(50, max = 0.1)),
y = cumsum(runif(50, max = 0.1))
)
ggplot(dat, aes(x = x, y = y)) +
geom_polygon(aes(color = "Border", group = …Run Code Online (Sandbox Code Playgroud)