How to Easily Create Barplots with Error Bars in R

plot of chunk bar-plot-with-error-bars-in-r


How to Easily Create Barplots with Error Bars in R

This article describes how to easily create barplots with error bars in R using the ggpubr package, an extension of ggplot2 for creating publication ready plots.

The following R code produces barplots showing means +/- sd and the jittered data points for each group.

library(ggpubr)

# Create a simple bar plot
ggbarplot(
  ToothGrowth, x = "dose", y = "len", 
  add = c("mean_se", "jitter"),
  fill = "#BF504D"
  )

plot of chunk bar-plot-with-error-bars-in-r

# Grouped bar plots
# Colored by groups
ggbarplot(
  ToothGrowth, x = "dose", y = "len", 
   add = c("mean_sd", "jitter"), 
   color = "supp", palette = c("#807F7F", "#BF504D"),
   position = position_dodge(0.8)
  )

plot of chunk bar-plot-with-error-bars-in-r

# Change bars fill color by groups
# You need to change also point shapes by groups
ggbarplot(
  ToothGrowth, x = "dose", y = "len", 
   add = c("mean_sd", "jitter"), 
   add.params = list(shape = "supp"),
   fill= "supp", palette = c("#807F7F", "#BF504D"),
   position = position_dodge(0.8)
  )

plot of chunk bar-plot-with-error-bars-in-r

See other related frequently questions: ggpubr FAQ



Version: Français





No Comments

Give a comment

Want to post an issue with R? If yes, please make sure you have read this: How to Include Reproducible R Script Examples in Datanovia Comments