我尝试在每个faceted_wrap图形的partial geom_sf plot中添加直方图子图,使用与相应的facet_wrap图相同的数据。
我在谷歌上找到了一些方法,但目前还没有实质性进展。
我的先前尝试:
library(sf)
library(ggplot2)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
nc <- rbind(nc, nc[rep(1:100, 3), ])
nc <- nc[order(nc$NAME),]
nc$GROUP <- c("A", "B", "C", "D")
nc$VALUE <- runif(400, min=0, max=10)
main <- ggplot() +
geom_sf(data = nc,
aes(fill = VALUE),
color = NA) +
scale_fill_gradientn(colours = c("#f3ff2c", "#96ffea", "#00429d"),
guide = "colorbar") +
coord_sf(datum = NA) +
theme(panel.background = element_blank(),
strip.background = element_blank(),) +
facet_wrap(~ GROUP, nrow = 2)
sub <- ggplot(nc, aes(x=VALUE)) +
geom_histogram(binwidth = 1) +
theme_minimal(base_size=9) +
theme(panel.background = element_blank(),
strip.background = element_blank(),) +
facet_wrap(~ GROUP, nrow = 2)
main + annotation_custom(grob = ggplotGrob(sub))
有什么方法可以实现这个目标吗?
patchwork
中新的inset_element
函数的好机会。最好的祝福,S。 - stefan