Search code examples
rggplot2errorbar

how to put standard error bars on my ggplot2 2-factor bar graphic?


I've been trying for a while to change my current standard deviation bars into standard error bars in my bar plot.

This is the plot with mean+sd:

EGG <- data.frame(type = c("this", "this", "that", "that"),
                  chemcon = c(10,11,12,13),
                  day = c("Monday", "Tuesday", "Monday", "Tuesday"))

EGG
#>    type chemcon     day
#> 1  this      10  Monday
#> 2  this      11 Tuesday
#> 3  that      12  Monday
#> 4  that      13 Tuesday

require(ggplot2)
aa <- aggregate(chemcon ~ day + type, data=EGG, FUN=mean)
bb <- aggregate(chemcon ~ day + type, data=EGG, FUN=sd)
cc <- merge(aa, bb, by=c("day", "type"))
colnames(cc)[3:4] <- c("mean", "sd")

ggplot(cc, aes(x = type, y = mean, fill = day))+
  geom_bar(stat="identity", position= "dodge") + #nb you can just use 'dodge' in barplots
  scale_fill_brewer(palette="Paired")+
  theme_minimal() +
  labs(x="", y="chemcon") +
  theme(panel.background = element_blank(),
        axis.line = element_line(colour = "black"),
        panel.grid=element_blank()) +
  geom_errorbar(aes(ymin = mean-sd,
                    ymax = mean+sd), 
                position = "dodge")

I tried to replace the function (FUN) of aggregate of "sd" with "se", no luck, then I tried to create "se" also with no luck:

se = sd(Egg$chemcon) / sqrt(length(Egg$chemcon))

The issue is because I must keep the "aggregate" function due to it working the best when trying to represent a two-factor bar plot, but haven't I seen anyone use it quite like this anywhere else with standard error. Can someone please help me understand what I'm missing?


Solution

  • Try std.error() from plotrix package as shown below

    library(plotrix)
    library(ggplot2)
    EGG <- data.frame(type = rep(c("this", "this", "that", "that"),2),
                      chemcon = c(10,11,12,13,14,15,16,17),
                      day = rep(c("Monday", "Tuesday"),4))
    
    EGG
    
    aa <- aggregate(chemcon ~ day + type, data=EGG, FUN=mean)
    bb <- aggregate(chemcon ~ day + type, data=EGG, FUN=sd)
    ee <- aggregate(chemcon ~ day + type, data=EGG, FUN=std.error)  ##  from library(plotrix)
    #ee <- aggregate(chemcon ~ day + type, data=EGG, FUN=sd(x)/sqrt(sum(!is.na(x))) )
    # cc <- aggregate(chemcon ~ day + type, data=EGG, FUN = function(x) c(mean = mean(x), sd = sd(x), se = (sd(x)/sqrt(sum(!is.na(x)))) ))
    cc <- merge(aa, ee, by=c("day", "type"))
    colnames(cc)[3:4] <- c("mean", "se")
    
    
    ggplot(cc, aes(x = type, y = mean, fill = day))+
      geom_bar(stat="identity", position= "dodge") + #nb you can just use 'dodge' in barplots
      scale_fill_brewer(palette="Paired")+
      theme_minimal() +
      labs(x="", y="chemcon") +
      theme(panel.background = element_blank(),
            axis.line = element_line(colour = "black"),
            panel.grid=element_blank()) +
      geom_errorbar(aes(ymin = mean-se,
                        ymax = mean+se), 
                    position = "dodge")
    

    output