如何将一组 grobs(ximin、xmax、ymin、ymax)的参数从 data.frame 传递给 annotation_custom 函数(ggplot2)。下面的例子说明了这个问题。
# Grobs
a <- ggplot(diamonds, aes(carat)) + geom_histogram(fill = "red") + theme_void()
g1 <- ggplotGrob(a)
b <- ggplot(diamonds, aes(carat)) + geom_histogram(fill = "blue") + theme_void()
g2 <- ggplotGrob(b)
# Structure of the arguments location
my.data <- data.frame(grob = c("g1", "g2"), xmin = c(1.5, 5.5), xmax = c(4.5, 8),
ymin = c(2.5, 4), ymax = c(6, 8))
# Desired result
df <- data.frame(x = 1:10, y = 1:10)
ggplot(df, aes(x, y)) + geom_blank() + theme_bw() +
annotation_custom(g1, xmin = 1.5, xmax = 4.5, ymin = 2.5, ymax = 6) +
annotation_custom(g2, xmin = 5.5, xmax = 8, ymin = 4, ymax = 8)