Skip to content

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.

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.4
2 S02 Control Batch1 9.15 83.3
3 S03 Control Batch1 10.5 80.7
# ℹ 57 more rows

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"
)

Boxplots of expression for Control, Drug_A and Drug_B with the 20 samples jittered over each box. The label Anova, p = 0.0032 sits at the top left. Three brackets mark the Holm-adjusted t-tests: Control against Drug_A is marked with two stars, and the other two pairs are marked ns.

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-test
2 Control Drug_B 0.0368 0.074 * T-test
3 Drug_A Drug_B 0.121 0.12 ns T-test

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"
)

Boxplots outlined in red, blue and green with the samples as colored points. Two brackets start at Control: the bracket to Drug_A reads 0.0046 and the bracket to Drug_B reads 0.0368.

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"
)

Violin plots of viability for the three groups, trimmed at the most extreme samples, with a white boxplot inside each. Control has the highest median near 80 percent and Drug_A the lowest near 73 percent. The Control against Drug_A bracket shows one star and the other two brackets show ns.

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"
)

Bar chart of mean expression, about 10.3 for Control, 12.5 for Drug_A and 11.5 for Drug_B, with standard deviation error bars and the individual samples jittered over each bar.

A bar that starts at zero hides the spread of the samples. The jittered points put it back.

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"
)

Dot plot with every sample stacked as a filled circle in its group, and a black point with a vertical line for the mean and standard deviation. The Drug_A mean sits highest near 12.4.

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"
)

Scatter plot of viability against expression with points colored by group, one black regression line with a grey confidence band, and the label R = -0.87, p < 2.2e-16. Viability falls from about 93 percent at expression 6 to about 60 percent at expression 17.

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-test

ggpaired() 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"
)

Paired plot of 12 patients with a blue Baseline box and a yellow Week4 box. A grey line joins the two measurements of each patient, and every line rises or stays level. The bracket above reads Paired t-test, p = 0.00017.

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"
)

Two panels, Batch1 and Batch2, each with Control, Drug_A and Drug_B boxplots. The label in Batch1 reads Anova, p = 0.068 and the label in Batch2 reads Anova, p = 0.044.

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"
)

Three QQ plots, one per group, with sample quantiles against theoretical quantiles, a reference line and a shaded confidence band. Most points fall inside the bands, and the lowest Drug_B sample near 7 falls just below its band.

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"
)

The same boxplot drawn three times. The npg palette fills the boxes red, light blue and teal, the lancet palette fills them dark blue, red and green, and the custom palette fills them teal, yellow and orange.

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"
)

Two-panel figure labelled A and B. Panel A is the boxplot with the Anova label and pairwise brackets, panel B is the expression against viability scatter plot, and one shared group legend sits below both panels.

The ggpubr package by Kassambara builds publication-ready plots on ggplot2.

  • ggpubr pairwise comparisons

    The reference explains test methods, bracket positions, adjusted labels and the scope of p-value correction.