Search code examples
rggplot2errorbar

increasing the limit for the y-axis on ggplot2 plus putting letters above error bars


I've been trying to set the limit for the y-axis above the letters I have on top of the graph bars for awhile now, and while I thought the answer had to do with the function ylim(), I've had no luck and keep getting errors. Furthermore, I noticed the letters are equally distanced from the bars but not the error bars, which I also need. Any help would be greatly appreciated.

Number <- c("a", "b", "c", "a","b","c", "a", "b", "c")

aa1 <- aggregate(total ~ class + treatment, data=lipidclasses_UFA_SFA, FUN=mean)
bb1 <- aggregate(total ~ class + treatment, data=lipidclasses_UFA_SFA, FUN=sd)
ee1 <- aggregate(total ~ class + treatment, data=lipidclasses_UFA_SFA, FUN=std.error)
cc1 <- merge(aa1, ee1, by=c("class", "treatment"))
colnames(cc1)[3:4] <- c("mean", "se")


ggplot(cc1, aes(x = class, y = mean, fill = treatment))+
  geom_bar(stat="identity", position= "dodge") + 
  scale_fill_brewer(palette="Paired")+
  theme_minimal() +
  labs(x="", y="UFA/SFA ratio", title="") +
  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")+
  geom_text(aes(label=Number), position=position_dodge(width=0.9), vjust=-1.5, size = 4) 

dput and str for reference

> dput(lipidclasses_UFA_SFA)
structure(list(all = c("Zn100_1", "Zn100_2", "Zn100_3", "Zn100_1", 
"Zn100_2", "Zn100_3", "Zn100_1", "Zn100_2", "Zn100_3", "nZn100_1", 
"nZn100_2", "nZn100_3", "nZn100_1", "nZn100_2", "nZn100_3", "nZn100_1", 
"nZn100_2", "nZn100_3", "control_1", "control_2", "control_3", 
"control_1", "control_2", "control_3", "control_1", "control_2", 
"control_3"), treatment = c("Zn100", "Zn100", "Zn100", "Zn100", 
"Zn100", "Zn100", "Zn100", "Zn100", "Zn100", "nZn100", "nZn100", 
"nZn100", "nZn100", "nZn100", "nZn100", "nZn100", "nZn100", "nZn100", 
"control", "control", "control", "control", "control", "control", 
"control", "control", "control"), conc = c(100, 100, 100, 100, 
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 
100, 0, 0, 0, 0, 0, 0, 0, 0, 0), exp = c("Zn", "Zn", "Zn", "Zn", 
"Zn", "Zn", "Zn", "Zn", "Zn", "nZn", "nZn", "nZn", "nZn", "nZn", 
"nZn", "nZn", "nZn", "nZn", "control", "control", "control", 
"control", "control", "control", "control", "control", "control"
), class = c("PC", "PC", "PC", "PE", "PE", "PE", "NL", "NL", 
"NL", "PC", "PC", "PC", "PE", "PE", "PE", "NL", "NL", "NL", "PC", 
"PC", "PC", "PE", "PE", "PE", "NL", "NL", "NL"), total = c(1.38328880252611, 
1.4911119212467, 1.30925105528925, 1.62752721617418, 1.66334107002825, 
1.53961527416302, 1.90704879938033, 1.96359284822576, 1.78637077287962, 
2.18415094339623, 2.01822098644824, 2.15015164948669, 0.956319702602231, 
1.05903443553121, 1.04794720409295, 1.85743801652893, 1.76054349127779, 
2.01839145659027, 2.93110575329528, 2.62488332954897, 2.83142533837198, 
1.53400673400673, 1.57560352127959, 1.78640128763789, 2.01213697442566, 
2.02762423717455, 1.84084369965078)), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -27L))
> str(lipidclasses_UFA_SFA)
tibble [27 × 6] (S3: tbl_df/tbl/data.frame)
 $ all      : chr [1:27] "Zn100_1" "Zn100_2" "Zn100_3" "Zn100_1" ...
 $ treatment: chr [1:27] "Zn100" "Zn100" "Zn100" "Zn100" ...
 $ conc     : num [1:27] 100 100 100 100 100 100 100 100 100 100 ...
 $ exp      : chr [1:27] "Zn" "Zn" "Zn" "Zn" ...
 $ class    : chr [1:27] "PC" "PC" "PC" "PE" ...
 $ total    : num [1:27] 1.38 1.49 1.31 1.63 1.66 ...

Solution

  • For the text, give it a new y aesthetic according to the same formula for top of error bars. ylim is called like so:

    library(ggplot2)
    
    ggplot(cc1, aes(x = class, y = mean, fill = treatment))+
      geom_bar(stat="identity", position= "dodge") + 
      scale_fill_brewer(palette="Paired")+
      theme_minimal() +
      labs(x="", y="UFA/SFA ratio", title="") +
      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")+
      geom_text(aes(label=Number, y = mean+se),
                position=position_dodge(width=0.9), vjust=-1.5, size = 4) +
      ylim(0, 4)
    

    If there were particular errors you found with ylim can you add code of how you used it and what the errors were?