This article describes how to create easily basic and ordered bar plots using ggplot2 based helper functions available in the ggpubr R package. We’ll also present some modern alternatives to bar plots, including lollipop charts and cleveland’s dot plots.
Note that, the approach to build a bar plot, using ggplot2 standard verbs, has been described in our previous article available at: ggplot2 barplots : Quick start guide.
You might be also interested by the following articles:
Add P-values and Significance Levels to ggplots
Facilitating Exploratory Data Visualization: Application to TCGA Genomic Data
Bar plots and modern alternatives
Contents:
Prerequisites
Basic bar plots
Multiple grouping variables
Ordered bar plots
Deviation graphs
Alternatives to bar plots
Infos
Prerequisites
Required R package
You need to install the R package ggpubr (version __= 0.1.3), to easily create ggplot2-based publication ready plots.
Install from CRAN:
install.packages(&quot;ggpubr&quot;)
Or, install the latest developmental version from GitHub as follow:
if(!require(devtools)) install.packages(&quot;devtools&quot;)
devtools::install_github(&quot;kassambara/ggpubr&quot;)
Load ggpubr:
library(ggpubr)
Basic bar plots
Create a demo data set:
df dose len
1 D0.5 4.2
2 D1 10.0
3 D2 29.5
Basic bar plots:
# Basic bar plots with label
p Bar plots and modern alternatives
Change fill and outline colors by groups:
ggbarplot(df, x = &quot;dose&quot;, y = &quot;len&quot;,
fill = &quot;dose&quot;, color = &quot;dose&quot;, palette = &quot;jco&quot;)
Bar plots and modern alternatives
Multiple grouping variables
Create a demo data set:
df2 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
Plot y = “len” by x = “dose” and change color by a second group: “supp”
# Stacked bar plots, add labels inside bars
ggbarplot(df2, x = &quot;dose&quot;, y = &quot;len&quot;,
fill = &quot;supp&quot;, color = &quot;supp&quot;, palette = c(&quot;gray&quot;, &quot;black&quot;),
label = TRUE, lab.col = &quot;white&quot;, lab.pos = &quot;in&quot;)
# Change position: Interleaved (dodged) bar plot
ggbarplot(df2, x = &quot;dose&quot;, y = &quot;len&quot;,
fill = &quot;supp&quot;, color = &quot;supp&quot;, palette = c(&quot;gray&quot;, &quot;black&quot;),
position = position_dodge(0.9))
Bar plots and modern alternatives
Ordered bar plots
Load and prepare data:
# Load data
data(&quot;mtcars&quot;)
dfm name wt mpg cyl
Mazda RX4 Mazda RX4 2.620 21.0 6
Mazda RX4 Wag Mazda RX4 Wag 2.875 21.0 6
Datsun 710 Datsun 710 2.320 22.8 4
Hornet 4 Drive Hornet 4 Drive 3.215 21.4 6
Hornet Sportabout Hornet Sportabout 3.440 18.7 8
Valiant Valiant 3.460 18.1 6
Create ordered bar plots. Change the fill color by the grouping variable “cyl”. Sorting will be done globally, but not by groups.
ggbarplot(dfm, x = &quot;name&quot;, y = &quot;mpg&quot;,
fill = &quot;cyl&quot;, # change fill color by cyl
color = &quot;white&quot;, # Set bar border colors to white
palette = &quot;jco&quot;, # jco journal color palett. see ?ggpar
sort.val = &quot;desc&quot;, # Sort the value in dscending order
sort.by.groups = FALSE, # Don't sort inside each group
x.text.angle = 90 # Rotate vertically x axis texts
)
Bar plots and modern alternatives
Sort bars inside each group. Use the argument sort.by.groups = TRUE.
ggbarplot(dfm, x = &quot;name&quot;, y = &quot;mpg&quot;,
fill = &quot;cyl&quot;, # change fill color by cyl
color = &quot;white&quot;, # Set bar border colors to white
palette = &quot;jco&quot;, # jco journal color palett. see ?ggpar
sort.val = &quot;asc&quot;, # Sort the value in dscending order
sort.by.groups = TRUE, # Sort inside each group
x.text.angle = 90 # Rotate vertically x axis texts
)
Bar plots and modern alternatives
Deviation graphs
The deviation graph shows the deviation of quantitative values to a reference value. In the R code below, we’ll plot the mpg z-score from the mtcars data set.
Calculate the z-score of the mpg data:
# Calculate the z-score of the mpg data
dfm$mpg_z name wt mpg mpg_z mpg_grp cyl
Mazda RX4 Mazda RX4 2.620 21.0 0.1508848 high 6
Mazda RX4 Wag Mazda RX4 Wag 2.875 21.0 0.1508848 high 6
Datsun 710 Datsun 710 2.320 22.8 0.4495434 high 4
Hornet 4 Drive Hornet 4 Drive 3.215 21.4 0.2172534 high 6
Hornet Sportabout Hornet Sportabout 3.440 18.7 -0.2307345 low 8
Valiant Valiant 3.460 18.1 -0.3302874 low 6
Create an ordered bar plot, colored according to the level of mpg:
ggbarplot(dfm, x = &quot;name&quot;, y = &quot;mpg_z&quot;,
fill = &quot;mpg_grp&quot;, # change fill color by mpg_level
color = &quot;white&quot;, # Set bar border colors to white
palette = &quot;jco&quot;, # jco journal color palett. see ?ggpar
sort.val = &quot;asc&quot;, # Sort the value in ascending order
sort.by.groups = FALSE, # Don't sort inside each group
x.text.angle = 90, # Rotate vertically x axis texts
ylab = &quot;MPG z-score&quot;,
xlab = FALSE,
legend.title = &quot;MPG Group&quot;
)
Bar plots and modern alternatives
Rotate the plot: use rotate = TRUE and sort.val = “desc”
ggbarplot(dfm, x = &quot;name&quot;, y = &quot;mpg_z&quot;,
fill = &quot;mpg_grp&quot;, # change fill color by mpg_level
color = &quot;white&quot;, # Set bar border colors to white
palette = &quot;jco&quot;, # jco journal color palett. see ?ggpar
sort.val = &quot;desc&quot;, # Sort the value in descending order
sort.by.groups = FALSE, # Don't sort inside each group
x.text.angle = 90, # Rotate vertically x axis texts
ylab = &quot;MPG z-score&quot;,
legend.title = &quot;MPG Group&quot;,
rotate = TRUE,
ggtheme = theme_minimal()
)
Bar plots and modern alternatives
Alternatives to bar plots
Lollipop chart
Lollipop chart is an alternative to bar plots, when you have a large set of values to visualize.
Lollipop chart colored by the grouping variable “cyl”:
ggdotchart(dfm, x = &quot;name&quot;, y = &quot;mpg&quot;,
color = &quot;cyl&quot;, # Color by groups
palette = c(&quot;#00AFBB&quot;, &quot;#E7B800&quot;, &quot;#FC4E07&quot;), # Custom color palette
sorting = &quot;ascending&quot;, # Sort value in descending order
add = &quot;segments&quot;, # Add segments from y = 0 to dots
ggtheme = theme_pubr() # ggplot2 theme
)
Bar plots and modern alternatives
Sort in descending order. sorting = “descending”.
Rotate the plot vertically, using rotate = TRUE.
Sort the mpg value inside each group by using group = “cyl”.
Set dot.size to 6.
Add mpg values as label. label = “mpg” or label = round(dfm$mpg).
ggdotchart(dfm, x = &quot;name&quot;, y = &quot;mpg&quot;,
color = &quot;cyl&quot;, # Color by groups
palette = c(&quot;#00AFBB&quot;, &quot;#E7B800&quot;, &quot;#FC4E07&quot;), # Custom color palette
sorting = &quot;descending&quot;, # Sort value in descending order
add = &quot;segments&quot;, # Add segments from y = 0 to dots
rotate = TRUE, # Rotate vertically
group = &quot;cyl&quot;, # Order by groups
dot.size = 6, # Large dot size
label = round(dfm$mpg), # Add mpg values as dot labels
font.label = list(color = &quot;white&quot;, size = 9, vjust = 0.5), # Adjust label parameters
ggtheme = theme_pubr() # ggplot2 theme
)
Bar plots and modern alternatives
Deviation graph:
Use y = “mpg_z”
Change segment color and size: add.params = list(color = “lightgray”, size = 2)
ggdotchart(dfm, x = &quot;name&quot;, y = &quot;mpg_z&quot;,
color = &quot;cyl&quot;, # Color by groups
palette = c(&quot;#00AFBB&quot;, &quot;#E7B800&quot;, &quot;#FC4E07&quot;), # Custom color palette
sorting = &quot;descending&quot;, # Sort value in descending order
add = &quot;segments&quot;, # Add segments from y = 0 to dots
add.params = list(color = &quot;lightgray&quot;, size = 2), # Change segment color and size
group = &quot;cyl&quot;, # Order by groups
dot.size = 6, # Large dot size
label = round(dfm$mpg_z,1), # Add mpg values as dot labels
font.label = list(color = &quot;white&quot;, size = 9, vjust = 0.5), # Adjust label parameters
ggtheme = theme_pubr() # ggplot2 theme
)+
geom_hline(yintercept = 0, linetype = 2, color = &quot;lightgray&quot;)
Bar plots and modern alternatives
Cleveland’s dot plot
Color y text by groups. Use y.text.col = TRUE.
ggdotchart(dfm, x = &quot;name&quot;, y = &quot;mpg&quot;,
color = &quot;cyl&quot;, # Color by groups
palette = c(&quot;#00AFBB&quot;, &quot;#E7B800&quot;, &quot;#FC4E07&quot;), # Custom color palette
sorting = &quot;descending&quot;, # Sort value in descending order
rotate = TRUE, # Rotate vertically
dot.size = 2, # Large dot size
y.text.col = TRUE, # Color y text by groups
ggtheme = theme_pubr() # ggplot2 theme
)+
theme_cleveland() # Add dashed grids
Bar plots and modern alternatives
Infos
This analysis has been performed using R software (ver. 3.3.2) and ggpubr (ver. 0.1.4).