我想在我的facet_print图的末尾添加一个摘要图,如果我没有使用facet_print,那么这个图会是什么样子.例如:

test=data.frame(x=runif(1000,min=-1,max=1),y=as.character(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)

这将生成以下图形:

histogram of random data with a facet_wrap

然而,由于我在右下角有一个额外的角,所以我想添加所有数据的汇总图,就好像我没有使用变量y一样.它将如下所示:

ggplot(test, aes(x=x, fill=x)) +
      geom_histogram(show.legend = FALSE)+
      theme_minimal()+
      ggtitle('All Data')

All Random data in Histogram

有没有办法做到这一点,也许是使用GRID.安排?

干杯!

编辑以调整测试数据以更好地代表真实数据:

请注意,对于假数据,以下代码可以工作,但不能使用我的真实数据.

bins <- 200
cols <- c("#0072B2",'#253f4b',"#0072B2","#D55E00","#F0E442","#D55E00")
colGradient <- colorRampPalette(cols)
cut.cols <- colGradient(bins)
cuts <- cut

ggplot(test, aes(x=x, fill=cut(x,bins))) +
  geom_histogram(show.legend = FALSE, binwidth = 0.06)+
  geom_histogram(show.legend = FALSE,
    data = ~ transform(.x, y = "All Data")
  ) +
  scale_color_manual(values=cut.cols,labels=levels(cuts)) +
  scale_fill_manual(values=cut.cols,labels=levels(cuts)) +
  scale_x_continuous(breaks=c(-1,0,1), labels=c('dusk', 'dawn','dusk'), limits = c(-1.1,1.1))+
  
  theme_minimal()+
  facet_wrap(~y)

标准化时间==x,站点==y

推荐答案

要达到你想要的效果,一种 Select 是使用第二个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)

UPDATE是根据您通过dput提供的数据改编的代码.我仍然不明白,当您将代码应用于数据时,出现错误的原因是什么.注意:我修改了代码,将"All Data"类别放在最后.此外,我使用了一种不同的方法来用after_stat()给条形图上色.

library(ggplot2)

DPHdol$station <- factor(
  DPHdol$station,
  levels = c(unique(DPHdol$station), "All Data")
)

bins <- 100
cols <- c("#0072B2", "#253f4b", "#0072B2", "#D55E00", "#F0E442", "#D55E00")
colGradient <- colorRampPalette(cols)
cut.cols <- colGradient(bins)
cuts <- cut

ggplot(DPHdol, aes(x = normalizedtime)) +
  geom_histogram(
    aes(fill = after_stat(factor(x))),
    bins = bins
  ) +
  geom_histogram(
    aes(fill = after_stat(factor(x))),
    data = ~ transform(.x, station = factor("All Data", levels = levels(station))), 
    bins = bins
  ) +
  scale_color_manual(values = cut.cols, labels = levels(cuts)) +
  scale_fill_manual(values = cut.cols, labels = levels(cuts)) +
  scale_x_continuous(
    breaks = c(-1, 0, 1), 
    labels = c("dusk", "dawn", "dusk"),
    limits = c(-1.1, 1.1)
  ) +
  theme_minimal() +
  facet_wrap(~station) +
  guides(fill = "none")

R相关问答推荐

强制相关图以显示相关矩阵图中的尾随零

如何使用ggplot重新绘制LASO回归图?

如果行和列名以相同的开头,将矩阵值设置为0

按R中的组查找相邻列的行累积和的最大值

过滤器数据.基于两列的帧行和R中的外部向量

为什么在ggplot2中添加geom_text这么慢?

如何计算多个日期是否在一个日期范围内

我如何才能找到FAMILY=POISSON(LINK=&Q;LOG&Q;)中的模型预测指定值的日期?

如何在R forestplot中为多条垂直线分配唯一的 colored颜色 ?

比较理论阿尔法和经验阿尔法

Data.table';S GForce-将多个函数应用于多列(带可选参数)

TreeNode打印 twig 并为其上色

减少雨云面之间的间距并绘制所有统计数据点

是否有可能从边界中找到一个点值?

按两个因素将观测值分组后计算单独的百分比

随机将数据帧中特定列上的某些行设置为NA

条形图中的条形图没有try 赋予它们的 colored颜色

根据排名的顶点属性调整曲线图布局(&Q)

Ggplot2:添加更多特定 colored颜色 的线条

真实世界坐标的逆st_变换