Bar plot of Group Means with Individual Observations

October 27, 2016
By

(This article was first published on Easy Guides, and kindly contributed to R-bloggers)

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

Bar plot of Group Means with Individual Observations


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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)