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

时间:2019-09-03 16:30:46

标签: r ggplot2 bar-chart line-plot

我有这个问题: 对于几个变量,我有5个不同时期的五个平均值,而对于相同变量,我有1个总计平均值。 我想用小平面包装为同一图中的每个变量绘制一个图形,在中间的一个条形图(总平均值)上有五个点(周期平均)。

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

到目前为止,对于每个变量,我只获得带有五点线图的构面图。 我试图添加barplot(代码中显示了a),但结果不理想。

这是数据集的子集

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")

我希望我会附上类似的图形。 An example of the output that I wish to get

2 个答案:

答案 0 :(得分:1)

要想出一个动态的解决方案有点困难,因为您真正想要的是条形图的辅助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")

enter image description here

编辑:

这里是创建图例的附加代码。我不确定您想要的条形/线条,因此我留下了可以替换的占位符名称。

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())

enter image description here

答案 1 :(得分:1)

这是使用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")

{{1}}

enter image description here

相关问题