如何在 ggplot 中使用 facet_wrap 绘制 5 点线图和 1 点条形图

How plot 5 point lineplot and 1 point barplot for with facet_wrap in ggplot

我有这个问题: 我有几个变量的 5 个不同时期的五个平均值和相同变量的 1 个总平均值。 我想要一个带有小平面换行的图,为同一图中的每个变量绘制一个图表,在中间的一个条形图(总平均值)上有五个点(周期平均值)。

在 r 中将数据集格式化为 facet_wrap,无法修复中间的单点条形图。

到目前为止,我只得到了带有每个变量的五点线图的小平面图。 我尝试添加条形图(代码中显示),但没有结果。

这是数据集的一个子集

library(ggplot2)

facetperiodmean <- structure(list(phen = c("GS0", "GS1", "GS2", "GS3", "H", "GS0", 
"GS1", "GS2", "GS3", "H", "GS0", "GS1", "GS2", "GS3", "H", "GS0", 
"GS1", "GS2", "GS3", "H"), var = c("mz31_flux", "mz31_flux", 
"mz31_flux", "mz31_flux", "mz31_flux", "mz33_flux", "mz33_flux", 
"mz33_flux", "mz33_flux", "mz33_flux", "mz39_flux", "mz39_flux", 
"mz39_flux", "mz39_flux", "mz39_flux", "mz42_flux", "mz42_flux", 
"mz42_flux", "mz42_flux", "mz42_flux"), value = c(-0.0019661978021978, 
-0.00308365560165975, -0.00142552201933405, -0.00296758288770053, 
0.00716088983050848, -0.0201807203084833, 0.00699548966597077, 
0.2982090471597, 0.763140160427808, 0.0115715254237288, 0.00969123297732893, 
-0.00171052598693697, 0.292541864951768, 0.660130481283422, 0.00825954802259888, 
-0.000685986850921274, -0.000174803516028957, -0.00120851710610932, 
0.00190823529411765, 0.00197547090395481), err = c(0.0018197542356471, 
0.00193485421273063, 0.00171100912064672, 0.0078142824736682, 
0.000572623655079707, 0.00891612382048675, 0.00865982415221607, 
0.0261407868072828, 0.150893410160645, 0.000915642610907682, 
0.00930308482712752, 0.0152763210173861, 0.0419053150085817, 
0.160356398727679, 0.00153078001050311, 0.000743192090385591, 
0.00108257092872938, 0.00109911506984063, 0.00586547846096966, 
0.000166849869755912), tot = c(-0.000531297508180212, NA, NA, 
NA, NA, 0.104295388186188, NA, NA, NA, NA, 0.104717855718061, 
NA, NA, NA, NA, -9.02923940837303e-05, NA, NA, NA, NA)), row.names = c(NA, 
20L), class = "data.frame")

到目前为止,该代码一直在尝试在绘图中间刻画一个单点条形图

ggplot(facetperiodmean,aes(x=phen,y=value,group=1))+
  geom_point(aes(colour=var),shape = 21, fill='darkolivegreen4',color='black',size = 1)+
  geom_line(color='darkolivegreen4', size = 0.5)+
  geom_errorbar(aes(ymin=value-err, ymax=value+err), width=0.5,
                position=position_dodge(0.9),color="darkolivegreen4")+
  ggplot(facetperiodmean,aes(x=phen,y=tot))+
  geom_bar(fill='darkolivegreen4',colour="black", stat="identity",width = 5,position=position_dodge(width = 20)) +
  facet_wrap(~var,scales="free_y")

我希望得到与我所附的图类似的东西。

想出一个动态的解决方案有点困难,因为您真正想要的是条形图的第二个 X 轴(因为条形图和线条自然不会位于同一 X 轴上)。

解决方法是手动指定绘图的中间位置(在您的例子中,中间的 x 值为 GS2)并将条形宽度设置为 5 以覆盖整个轴。

ggplot(facetperiodmean,aes(x=phen,y=value,group=1))+
  geom_bar(aes(x='GS2', y = tot), fill='darkolivegreen4',colour="black",
           stat="identity",position='dodge', width = 5) +
  geom_errorbar(aes(ymin=value-err, ymax=value+err), width=0.5, 
                position=position_dodge(0.9),colour="darkolivegreen4")+
  geom_line(colour="black", size = 0.5)+
  geom_point(aes(colour=var),shape = 21, fill='darkolivegreen4',color='black',size = 1)+
  facet_wrap(~var,scales="free_y")

编辑:

这是创建图例的附加代码。我不确定你想要怎样的条/线,所以我留下了你可以替换的占位符名称。

ggplot(facetperiodmean,aes(x=phen,y=value,group=1))+
  geom_bar(aes(x='GS2', y = tot, fill='Bar Label'),colour="black",
           stat="identity",position='dodge', width = 5) +
  geom_errorbar(aes(ymin=value-err, ymax=value+err), width=0.5, 
                position=position_dodge(0.9),colour="darkolivegreen4")+
  geom_line(aes(colour="line label"), size = 0.5)+
  geom_point(aes(colour='line label'),shape = 21, fill='darkolivegreen4',size = 1)+
  facet_wrap(~var,scales="free_y") +
  scale_color_manual(values = c('#000000'))+
  scale_fill_manual(values = c('darkolivegreen4'))+
  theme(legend.position = 'bottom',
        legend.title = element_blank())

这是一种使用 geom_rect 的方法,我们指定所有矩形的范围应从最小值到最大值 phen:

ggplot(facetperiodmean,aes(x=phen,y=value,group=1))+
  geom_rect(aes(xmin = min(phen), xmax = max(phen),
                ymin = 0, ymax = tot), fill='darkolivegreen4',colour="black", 
           stat="identity",width = 0.9, alpha = 0.3,
           position=position_dodge(width = 20)) +
  geom_point(aes(colour=var),shape = 21, fill='darkolivegreen4',color='black',size = 1)+
  geom_line(color='darkolivegreen4', size = 0.5) +
  geom_errorbar(aes(ymin=value-err, ymax=value+err), width=0.5,
                position=position_dodge(0.9),color="darkolivegreen4") +
  facet_wrap(~var,scales="free_y")