I try to add histogram suplots to each partial geom_sf plot of a facet_wrap plot based on the same data as in the respective facet_wrap plot.
I have found some approaches via Google, but so far nothing concrete.
My previous approach:
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))
any idea how i can achive this?
