Search code examples
rggplot2bar-chartline-plot

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


I have this problem: I have five averages for 5 different periods for several variables and 1 total average for the same variables. I want plot, with the facet wrap, a graph for each variables in the same figure, with five point (periods average) on one bar (total average) in the middle.

Formatting the dataset to facet_wrap in r, in can't fix the one-point barplot in the middle.

To date I've only get the facet plot with the five point lineplot for each variables. I tried to add the barplot (a showed in the code), but whitout results.

This a subset of the dataset

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

The code tried untill now to facet a onepoint barplot in the middle of the plot

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

I expect something like the figure that I'm attaching. An example of the output that I wish to get


Solution

  • It's a bit hard to come up with a dynamic solution since what you really want is a secondary X axis for the bar (since the bar and lines do not naturally sit on the same X axis).

    A workaround is to manually specify the middle of the plot (in your case, the middle x value is GS2) and set the width of the bar to 5 to cover the full axis.

    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

    EDIT:

    Here is the additional code to create a legend. I'm not sure how you want to the bars / lines so I have left placeholder names that you can replace.

    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