How to add error bars in grouped bar plot

60 Views Asked by At

I am trying to add error bars in a grouped bar plot, but I need help, and I only managed to add one for all the bars.

Here is the dataset:

structure(list(ATPS = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 
4L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L), levels = c("Bet/K[3]*PO[4]", 
"Bet:2*LA/K[3]*PO[4]", "ChCl/K[2]*HPO[4]", "ChCl/K[3]*PO[4]", 
"ChCl:2*Urea/K[2]*HPO[4]", "ChCl:2*Urea/K[3]*PO[4]"), class = "factor"), 
    Time = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 
    3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 
    1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L), levels = c("10", 
    "30", "120"), class = "factor"), PRY = c(13.0868624345375, 
    13.3069727436355, 14.2861734548006, 14.3971163292156, 19.8327442593556, 
    16.5065171582405, 15.3722991514246, 13.267337386624, 16.2339726154907, 
    16.5667642586239, 18.8102379269557, 15.3723095483792, 8.05216872389519, 
    6.74890507936567, 6.8770502098371, 5.94506104304842, 5.3492574026405, 
    6.29804228891332, 8.14092340374223, 8.4320853238649, 9.46607846912162, 
    9.63700172219074, 7.22690946294224, 6.36694213790807, 7.84596544446703, 
    9.23341231107417, 8.59073917556084, 7.77520021558161, 12.11550173028, 
    11.2753742595947, 6.38511064440406, 6.84800014471827, 8.17981607730215, 
    7.81842432992541, 8.38830693625029, 9.21742862814511), CRY = c(12.4711421505373, 
    12.491288159229, 15.2459238231991, 13.9584757582509, 19.7000892421615, 
    14.4718893113281, 17.6609911295163, 14.1135584981096, 16.039148888317, 
    16.9333881303311, 16.7672420855041, 14.1753388102187, 10.6788473200485, 
    10.555408781461, 12.6839479108831, 10.5969596174691, 12.3954911955203, 
    13.2936023290369, 11.4271288967229, 10.5078185041047, 13.106516837274, 
    12.5137401489965, 13.4873124030587, 13.9314810592219, 13.9391721707005, 
    13.9291813820727, 14.4633024101911, 12.4634609008938, 16.0384702210746, 
    12.4470632670647, 14.5328789715746, 14.4187420052722, 17.1849726169446, 
    14.8573186776821, 15.6803700874189, 15.5982040831611), PCR = c(1.04937160338387, 
    1.06530027760218, 0.937048723348719, 1.03142467548475, 1.0067337267138, 
    1.14059172255552, 0.870409765719958, 0.940041973709256, 1.0121467621836, 
    0.978349054017696, 1.12184447692908, 1.08444036182737, 0.754029763940725, 
    0.639378845395274, 0.542185308403579, 0.561015730705248, 
    0.4315486428302, 0.473764908339154, 0.712420720665616, 0.802458219141401, 
    0.722242117158142, 0.770113619704943, 0.535830211903691, 
    0.457018324960754, 0.562871693410812, 0.662882624456156, 
    0.593968025553255, 0.623839580146156, 0.755402576634785, 
    0.905866228657296, 0.439356211311808, 0.47493742118517, 0.475986564519558, 
    0.52623387163862, 0.534955928303034, 0.59092883892292)), row.names = c(NA, 
-36L), class = c("tbl_df", "tbl", "data.frame"))

Here is the code:

library(ggplot2)
library(ggpubr)
ATPStiempo$ATPS<-factor(ATPStiempo$ATPS)
ATPStiempo$Time<-factor(ATPStiempo$Time)
ATPStiempo$ATPS <- dplyr::recode(
  ATPStiempo$ATPS,
  "Bet:2LA / K3PO4" = "Bet:2*LA/K[3]*PO[4]",
  "ChCl:2Urea/K2HPO4" = "ChCl:2*Urea/K[2]*HPO[4]",
  "C2hCl:Urea/K3PO4" = "ChCl:2*Urea/K[3]*PO[4]",
  "Bet / K3PO4" = "Bet/K[3]*PO[4]",
  "ChCl / K3PO4" = "ChCl/K[3]*PO[4]",
  "ChCl / K2HPO4" = "ChCl/K[2]*HPO[4]"
)
ggplot(ATPStiempo,aes(x=ATPS,y=PCR))+
  geom_col(aes(fill = Time), position = position_dodge(0.75), width = 0.7)+
  geom_point(stat = 'summary', fun = mean,size=1)+
  stat_summary(fun.data="mean_sdl", fun.args = list(mult = 1), geom="errorbar",  width = 0.2, size=1, alpha = 0.5) +
  geom_hline(yintercept = mean(ATPStiempo$PCR, na.rm=TRUE),linetype='dotted', col = 'grey', size=1)+
  scale_fill_manual(name="Time / min",values = c("#00AFBB", "#E7B800","#CC79A7"))+
  theme_classic(base_size = 20) +
  theme(axis.text=element_text(size=16), axis.title=element_text(size=20,face="bold"), legend.title=element_text(size=20, face="bold"),legend.text=element_text(size=20))+     scale_x_discrete(labels = scales::label_parse())+labs(y = "Protein carbohydrate ratio", x= "ATPS")+ scale_y_continuous(expand = c(0, 0))

Here is an image of the actual compilation run, the red arrows indicate where I would like the additional error bars to be placed:

enter image description here

1

There are 1 best solutions below

6
On

you seem not to define your group in the aes() for your error bars.

You can put aes(group=interaction(ATPS, Time)) and position=position_dodge(0.9) in your stat_summary() where you produce error bars.