How to Create a GGPlot Horizontal Bar Chart

How to Create a GGPlot Horizontal Bar Chart

In this article, you will learn how to create a horizontal bar plot using the ggplot2 R package.

Related Book

GGPlot2 Essentials for Great Data Visualization in R


Load required packages and set the theme function theme_bw() as the default theme:


Data preparation

Data derived from ToothGrowth data sets are used. ToothGrowth describes the effect of Vitamin C on tooth growth in Guinea pigs. Three dose levels of Vitamin C (0.5, 1, and 2 mg) with each of two delivery methods [orange juice (OJ) or ascorbic acid (VC)] are used :

df <- data.frame(
  supp = rep(c("VC", "OJ"), each = 3),
  dose = rep(c("D0.5", "D1", "D2"), 2),
  len = c(6.8, 15, 33, 4.2, 10, 29.5)

##   supp dose  len
## 1   VC D0.5  6.8
## 2   VC   D1 15.0
## 3   VC   D2 33.0
## 4   OJ D0.5  4.2
## 5   OJ   D1 10.0
## 6   OJ   D2 29.5
  • len : Tooth length
  • dose : Dose in milligrams (0.5, 1, 2)
  • supp : Supplement type (VC or OJ)

Basic bar plots

p <- ggplot(df, aes(x = dose, y = len))+
  geom_col(aes(fill = supp), width = 0.7)

Horizontal bar chart

It’s very easy to create a horizontal bar chart.You just need to add the code coord_flip() after your bar chart code.

p + coord_flip()

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