我想在facet_wrap 图的末尾添加一个摘要图,说明如果我没有使用facet_wrap,该图会是什么样子。例如:
test=data.frame(x=sample(x=-100:100, size=1000, replace=TRUE),y=sample(x=1:8, size=1000, replace=TRUE))
ggplot(test, aes(x=x, fill=x)) +
geom_histogram(show.legend = FALSE)+
theme_minimal()+
facet_wrap(~y)
这产生了这个情节:
但是,由于我在右下角有那个额外的角,我想添加所有数据的汇总图,就好像我没有使用变量 y 一样。它看起来像这样:
ggplot(test, aes(x=x, fill=x)) +
geom_histogram(show.legend = FALSE)+
theme_minimal()+
ggtitle('All Data')
有没有办法做到这一点,也许使用 grid.arrange ?
干杯!
实现您期望的一个选择是使用第二个
geom_histogram
,如下所示:
library(ggplot2)
set.seed(123)
ggplot(test, aes(x = x)) +
geom_histogram() +
geom_histogram(
data = ~ transform(.x, y = "All Data")
) +
theme_minimal() +
facet_wrap(~y)