ggpubr
ggpubr wraps ggplot2 in one function per plot type and adds the test results to the figure.
Each gg*() function returns an ordinary ggplot, so + labs(), + theme(), and ggsave()
work as usual. The examples run in ggpubr 1.0.0.
Quick reference
Section titled “Quick reference”| Function | Plot | Key arguments |
|---|---|---|
ggboxplot() |
Boxplot | add = "jitter", palette |
ggviolin() |
Violin | add = "boxplot" |
ggbarplot() |
Bar of a summary | add = "mean_sd" |
ggdotplot() |
Stacked dots | binwidth, add = "mean_sd" |
ggstripchart() |
Jittered points | add = "mean_se" |
ggscatter() |
Scatter | add = "reg.line", conf.int |
ggpaired() |
Paired lines | id, line.color |
gghistogram() |
Histogram | bins, add = "mean" |
ggdensity() |
Density | add = "median" |
ggqqplot() |
QQ plot | facet.by |
ggarrange() |
Multi-panel | labels, common.legend |
| Statistics function | Use |
|---|---|
geom_pwc() |
Pairwise brackets with adjusted p-values |
stat_compare_means() |
Overall test, or one two-group test per panel |
stat_cor() |
Correlation coefficient and p-value |
stat_pvalue_manual() |
Brackets from a table you computed, such as rstatix output |
compare_means() |
The test results as a table, without a plot |
| Option | Values |
|---|---|
method in geom_pwc() |
"wilcox_test", "t_test", "dunn_test", "tukey_hsd", "games_howell_test" |
label |
"p.signif", "p.format", "p.adj.signif", "p.adj.format" |
p.format.style |
"default", "apa", "nejm", "lancet", "ama", "graphpad" |
The table holds one gene measured in 20 samples per treatment arm, with a viability readout that falls as expression rises.
library(ggpubr)library(dplyr)
set.seed(42)dir.create("outputs", showWarnings = FALSE)
experiment <- tibble( sample_id = sprintf("S%02d", 1:60), group = factor(rep(c("Control", "Drug_A", "Drug_B"), each = 20)), batch = rep(rep(c("Batch1", "Batch2"), each = 10), 3), expression = c(rnorm(20, 10, 1.5), rnorm(20, 13, 2), rnorm(20, 11.5, 1.5))) |> mutate(viability = 110 - 3 * expression + rnorm(60, 0, 4))print(experiment, n = 3)# A tibble: 60 × 5 sample_id group batch expression viability <chr> <fct> <chr> <dbl> <dbl>1 S01 Control Batch1 12.1 72.42 S02 Control Batch1 9.15 83.33 S03 Control Batch1 10.5 80.7# ℹ 57 more rowsBoxplot with adjusted pairwise p-values
Section titled “Boxplot with adjusted pairwise p-values”geom_pwc() runs every pairwise test, adjusts the p-values, and draws the brackets.
stat_compare_means(method = "anova") adds the overall test. Older tutorials pass
comparisons = to stat_compare_means(), which does not adjust the p-values for the number
of pairs.
box_stats <- ggboxplot( experiment, x = "group", y = "expression", fill = "group", palette = "npg", add = "jitter", add.params = list(size = 1.5, alpha = 0.6)) + geom_pwc( method = "t_test", p.adjust.method = "holm", label = "p.adj.signif" ) + stat_compare_means(method = "anova", label.y = 21) + labs(x = NULL, y = "Expression, log2 CPM") + theme(legend.position = "none")
ggsave( "outputs/ggpubr-boxplot-pwc.png", box_stats, width = 5.5, height = 5, dpi = 150, bg = "white")
The same tests as a table
Section titled “The same tests as a table”compare_means() returns the numbers behind the brackets, which you need for the figure
legend or a supplementary table. Its p.signif column is computed from the raw p, so report
p.adj when several pairs were tested.
compare_means( expression ~ group, data = experiment, method = "t.test", p.adjust.method = "holm") |> select(group1, group2, p, p.adj, p.signif, method)# A tibble: 3 × 6 group1 group2 p p.adj p.signif method <chr> <chr> <dbl> <dbl> <chr> <chr>1 Control Drug_A 0.00230 0.0069 ** T-test2 Control Drug_B 0.0368 0.074 * T-test3 Drug_A Drug_B 0.121 0.12 ns T-testCompare each group with a control
Section titled “Compare each group with a control”ref.group limits the tests to one reference level. Set hide.ns = TRUE to drop the brackets
that are not significant.
box_ref <- ggboxplot( experiment, x = "group", y = "expression", color = "group", palette = "npg", add = "jitter") + geom_pwc( method = "t_test", ref.group = "Control", p.adjust.method = "holm", label = "p.adj.format" ) + labs(x = NULL, y = "Expression, log2 CPM") + theme(legend.position = "none")
ggsave( "outputs/ggpubr-boxplot-ref.png", box_ref, width = 5.5, height = 5, dpi = 150, bg = "white")
Violin with a boxplot inside
Section titled “Violin with a boxplot inside”add = "boxplot" draws a narrow box inside each violin. trim = TRUE ends each violin at
the most extreme sample, so the brackets clear the tails. The Wilcoxon test is the default in
geom_pwc().
violin <- ggviolin( experiment, x = "group", y = "viability", fill = "group", palette = "jco", trim = TRUE, add = "boxplot", add.params = list(fill = "white", width = 0.1)) + geom_pwc(p.adjust.method = "holm", label = "p.adj.signif") + labs(x = NULL, y = "Viability, %") + theme(legend.position = "none")
ggsave( "outputs/ggpubr-violin.png", violin, width = 5.5, height = 5, dpi = 150, bg = "white")
Bar plot of the mean with error bars
Section titled “Bar plot of the mean with error bars”add = "mean_sd" summarises the raw rows, so you do not build a summary table first.
"mean_se" and "mean_ci" give the standard error and the 95% confidence interval.
bars <- ggbarplot( experiment, x = "group", y = "expression", fill = "group", palette = "npg", add = c("mean_sd", "jitter"), add.params = list(alpha = 0.5)) + labs(x = NULL, y = "Expression, log2 CPM") + theme(legend.position = "none")
ggsave( "outputs/ggpubr-barplot.png", bars, width = 5, height = 4.5, dpi = 150, bg = "white")
A bar that starts at zero hides the spread of the samples. The jittered points put it back.
Dot plot with mean and SD
Section titled “Dot plot with mean and SD”A dot plot stacks every sample, so the reader sees how many there are. A box drawn from six samples looks as solid as one drawn from sixty.
dots <- ggdotplot( experiment, x = "group", y = "expression", fill = "group", palette = "npg", binwidth = 0.4, add = "mean_sd", add.params = list(color = "black", size = 0.8)) + labs(x = NULL, y = "Expression, log2 CPM") + theme(legend.position = "none")
ggsave( "outputs/ggpubr-dotplot.png", dots, width = 5, height = 4.5, dpi = 150, bg = "white")
Scatter plot with a correlation
Section titled “Scatter plot with a correlation”add = "reg.line" fits a line with its confidence band. stat_cor() prints the coefficient
and its p-value. Use method = "spearman" for a rank correlation.
scatter <- ggscatter( experiment, x = "expression", y = "viability", color = "group", palette = "npg", add = "reg.line", conf.int = TRUE, add.params = list(color = "black", fill = "grey80")) + stat_cor(method = "pearson", label.x.npc = "center") + labs(x = "Expression, log2 CPM", y = "Viability, %", color = NULL)
ggsave( "outputs/ggpubr-scatter.png", scatter, width = 6, height = 4.5, dpi = 150, bg = "white")
Paired samples
Section titled “Paired samples”A paired design measures the same patient twice. compare_means(id = "patient") pairs the rows
by patient, so the test stays correct when the table is not sorted.
baseline <- rnorm(12, 9, 1.5)paired <- tibble( patient = rep(sprintf("P%02d", 1:12), times = 2), time = factor(rep(c("Baseline", "Week4"), each = 12)), expression = c(baseline, baseline + rnorm(12, 1, 0.8)))
paired_test <- compare_means( expression ~ time, data = paired, method = "t.test", paired = TRUE, id = "patient")paired_test |> select(group1, group2, p, method)# A tibble: 1 × 4 group1 group2 p method <chr> <chr> <dbl> <chr>1 Baseline Week4 0.000169 T-testggpaired() joins the two measurements of each patient with a line. stat_pvalue_manual()
draws the bracket from the table above. The id argument of stat_compare_means() drew no
label in ggpubr 1.0.0 when this page was tested, so the page passes the computed table
instead.
paired_plot <- ggpaired( paired, x = "time", y = "expression", id = "patient", fill = "time", palette = "jco", line.color = "grey60", line.size = 0.4) + stat_pvalue_manual( paired_test, y.position = 12, label = "Paired t-test, p = {p.format}" ) + labs(x = NULL, y = "Expression, log2 CPM") + theme(legend.position = "none")
ggsave( "outputs/ggpubr-paired.png", paired_plot, width = 4.5, height = 4.5, dpi = 150, bg = "white")
Facets with a test in each panel
Section titled “Facets with a test in each panel”facet.by splits the plot, and each statistics layer runs once per panel.
facets <- ggboxplot( experiment, x = "group", y = "expression", fill = "group", palette = "npg", facet.by = "batch") + stat_compare_means(method = "anova", label.y = 19) + labs(x = NULL, y = "Expression, log2 CPM") + theme(legend.position = "none")
ggsave( "outputs/ggpubr-facets.png", facets, width = 7, height = 4.5, dpi = 150, bg = "white")
Check normality
Section titled “Check normality”ggqqplot() draws the sample quantiles against the normal quantiles with a confidence band.
Points inside the band are consistent with a normal distribution.
qq <- ggqqplot( experiment, x = "expression", color = "group", palette = "npg", facet.by = "group") + theme(legend.position = "none")
ggsave( "outputs/ggpubr-qqplot.png", qq, width = 7, height = 3.5, dpi = 150, bg = "white")
Palettes
Section titled “Palettes”palette takes a journal palette name from the ggsci package, an RColorBrewer name, or a
vector of colors. The scale_fill_npg() family of functions lives in ggsci, not in ggpubr.
palette_plots <- lapply( list("npg", "lancet", c("#00AFBB", "#E7B800", "#FC4E07")), \(pal) ggboxplot( experiment, x = "group", y = "expression", fill = "group", palette = pal, legend = "none", xlab = FALSE, ylab = FALSE ))
palettes <- ggarrange( plotlist = palette_plots, ncol = 3, labels = c("npg", "lancet", "custom"), font.label = list(size = 11))
ggsave( "outputs/ggpubr-palettes.png", palettes, width = 9, height = 3.5, dpi = 150, bg = "white")
Arrange a multi-panel figure
Section titled “Arrange a multi-panel figure”ggarrange() places the plots on a grid and adds panel letters. common.legend = TRUE draws
one legend for all panels. The default align = "none" leaves each panel at its own size, so
set align = "h" when panels with different axis labels sit in one row.
figure <- ggarrange( box_stats, scatter, ncol = 2, labels = c("A", "B"), widths = c(1, 1.3), common.legend = TRUE, legend = "bottom")
ggsave( "outputs/ggpubr-arrange.png", figure, width = 10, height = 5, dpi = 150, bg = "white")
See also
Section titled “See also”- Two-Group Comparison and Multi-Group Comparison build the same figures with the test chosen and justified.
- Two Groups: t-test and Wilcoxon and Multiple Groups: ANOVA and Kruskal-Wallis explain the tests behind the brackets.
- Checking Assumptions covers the QQ plot and the normality tests.
- The rstatix cheatsheet computes the same tests as tables
and feeds them to
stat_pvalue_manual().
Scientific references
Section titled “Scientific references”The ggpubr package by Kassambara builds publication-ready plots on ggplot2.
Further reading
Section titled “Further reading”-
The reference explains test methods, bracket positions, adjusted labels and the scope of p-value correction.