Site icon R-bloggers

Bar plot of Group Means with Individual Observations

[This article was first published on Easy Guides, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
< !-- START HTML --> < !--====================== start from here when you copy to sthda================-->


ggpubr is great for data visualization and very easy to use for non-“R programmer”. It makes easy to simply produce an elegant ggplot2-based graphs. Read more about ggpubr: ggpubr .

Here we demonstrate how to plot easily a barplot of group means +/- standard error with individual observations.

Example data sets

d <- as.data.frame(mtcars[, c("am", "hp")])
d$rowname <- rownames(d)
head(d)
##                   am  hp           rowname
## Mazda RX4          1 110         Mazda RX4
## Mazda RX4 Wag      1 110     Mazda RX4 Wag
## Datsun 710         1  93        Datsun 710
## Hornet 4 Drive     0 110    Hornet 4 Drive
## Hornet Sportabout  0 175 Hornet Sportabout
## Valiant            0 105           Valiant

Install ggpubr

The latest version of ggpubr can be installed as follow:

# Install ggpubr
if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/ggpubr")

Bar plot of group means with individual informations

  • Plot y = “hp” by groups x = “am”
  • Add mean +/- standard error and individual points: add = c(“mean_se”, “point”). Allowed values are one or the combination of: “none”, “dotplot”, “jitter”, “boxplot”, “point”, “mean”, “mean_se”, “mean_sd”, “mean_ci”, “mean_range”, “median”, “median_iqr”, “median_mad”, “median_range”.
  • Color and fill by groups: color = “am”, fill = “am”
  • Add row names as labels.
library(ggpubr)
# Bar plot of group means + points
ggbarplot(d, x = "am", y = "hp",
          add = c("mean_se", "point"),
          color = "am", fill = "am", alpha = 0.5)+ 
  ggrepel::geom_text_repel(aes(label = rowname))

Bar plot of Group Means with Individual Observations

< !--end rdoc--> < !--====================== stop here when you copy to sthda================--> < !-- END HTML -->

To leave a comment for the author, please follow the link and comment on their blog: Easy Guides.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.