How to Add p-values onto ggplot
Using the R function stat_pvalue_manual() from the ggpubr R package, this article shows how to add p-values to ggplots.
The method coord_flip() [in the ggplot2 package] can be used to make horizontal charts.
The option coord.flip = TRUE in the function stat pvalue manual() [in the ggpubr package] must be specified when adding p-values to a horizontal ggplot.
To alter the location of the p-value labels, use the parameters vjust (vertical adjustment) and hjust (horizontal adjustment).
The p-value labels may be partially obscured by the plot top border in some cases. To add more space between labels and the plot top border, use the ggplot2 function scale_y_continuous(expand = expansion(mult = c(0, 0.1))).
The parameter mult = c(0, 0.1) indicates that 0% and 10% spaces are added to the bottom and top of the plot, respectively.
How to Add p-values onto ggplot
Make sure you have the necessary R packages installed:
ggpubr allows you to quickly create publication-ready plots.
For quick statistical analysis, rstatix provides pipe-friendly R functions.
Begin by installing the following packages and load it.
library(ggpubr) library(rstatix) df <- ToothGrowth
Convert the word ‘dosage’ into a factor variable.
df$dose <- as.factor(df$dose) head(df, 3)
  len supp dose  1 4.2  VC 0.5  2 11.5  VC 0.5  3 7.3  VC 0.5
Box plots are easy to make.
bxp <- ggboxplot(df, x = "dose", y = "len", fill = "dose",
                palette = c("#00AFBB", "#E7B800", "#FC4E07"))Bar plots showing mean +/- SD
bp <- ggbarplot(df, x = "dose", y = "len", add = "mean_sd", fill = "dose",
               palette = c("#00AFBB", "#E7B800", "#FC4E07"))Pairwise comparisons
Statistical test
In the following example, we’ll perform T-test using the function t_test() [rstatix package]. It’s also possible to use the function wilcox_test().
stat.test <- df %>% t_test(len ~ dose) stat.test
 A tibble: 3 x 10   .y.  group1 group2   n1   n2 statistic   df       p   p.adj p.adj.signif  * <chr> <chr> <chr> <int> <int>    <dbl> <dbl>   <dbl>   <dbl> <chr>       1 len  0.5   1        20   20    -6.48 38.0 1.27e- 7 2.54e- 7 ****        2 len  0.5   2        20   20   -11.8  36.9 4.40e-14 1.32e-13 ****        3 len  1     2        20   20    -4.90 37.1 1.91e- 5 1.91e- 5 ****
stat.test <- stat.test %>% add_xy_position(x = "dose") bxp + stat_pvalue_manual(stat.test, label = "p.adj.signif", tip.length = 0.01)
Bar plot
stat.test <- stat.test %>% add_xy_position(fun = "mean_sd", x = "dose") bp + stat_pvalue_manual(stat.test, label = "p.adj.signif", tip.length = 0.01)

Horizontal plots with p-values
Horizontal box plot with p-values using adjusted p-value significance levels as labels.
Decision Tree R Code » Classification & Regression »
stat.test <- stat.test %>% add_xy_position(x = "dose") bxp + Â stat_pvalue_manual( Â Â Â stat.test, label = "p.adj.signif", tip.length = 0.01, Â Â Â coord.flip = TRUE Â Â Â ) + Â coord_flip()

Horizontal bar plot with p-values
stat.test <- stat.test %>% add_xy_position(fun = "mean_sd", x = "dose") bp + stat_pvalue_manual( Â stat.test, label = "p.adj.signif", tip.length = 0.01, Â coord.flip = TRUE Â ) + Â coord_flip()

Assigning labels to the adjusted p-values Labeling with ‘p.adj’
stat.test <- stat.test %>% add_xy_position(x = "dose") bxp + Â stat_pvalue_manual( Â Â Â stat.test, label = "p.adj", tip.length = 0.01, Â Â Â coord.flip = TRUE Â Â Â ) + Â coord_flip()

Adjust the position of p-values using vjust and hjust Change the orientation angle of the p-values Increase the distance between the labels and the plot border.
bxp + Â stat_pvalue_manual( Â Â Â stat.test, label = "p.adj", tip.length = 0.01, Â Â Â coord.flip = TRUE, angle = 0, Â Â Â hjust = 0, vjust = c(0, 1.2, 0) Â Â Â ) + Â scale_y_continuous(expand = expansion(mult = c(0.05, 0.3))) + Â coord_flip()

Comparisons with control groups Statistical evaluations
How to Perform Univariate Analysis in R »
stat.test <- df %>% t_test(len ~ dose, ref.group = "0.5") stat.test
y.  group1 group2   n1   n2 statistic   df       p   p.adj p.adj.signif * <chr> <chr> <chr> <int> <int>    <dbl> <dbl>   <dbl>   <dbl> <chr>      1 len  0.5   1        20   20    -6.48 38.0 1.27e- 7 1.27e- 7 ****       2 len  0.5   2        20   20   -11.8  36.9 4.4 e-14 8.8 e-14 ****Â
p-values are shown vertically, p-values in a vertical box plot
stat.test <- stat.test %>% add_xy_position(x = "dose") bxp + Â stat_pvalue_manual(stat.test, label = "p.adj", tip.length = 0.01) + Â scale_y_continuous(expand = expansion(mult = c(0.05, 0.1)))

Vertical bar plot with p-values
stat.test <- stat.test %>% add_xy_position(fun = "mean_sd", x = "dose") bp + Â stat_pvalue_manual(stat.test, label = "p.adj.signif", tip.length = 0.01) + Â scale_y_continuous(expand = expansion(mult = c(0.05, 0.1)))

P-values in a horizontal box plot
stat.test <- stat.test %>% add_xy_position(x = "dose") bxp + Â stat_pvalue_manual( Â Â Â stat.test, label = "p.adj", tip.length = 0.01, Â Â Â coord.flip = TRUE Â Â Â ) + Â scale_y_continuous(expand = expansion(mult = c(0.05, 0.1))) + Â coord_flip()

Horizontal bar plot with p-values
stat.test <- stat.test %>% add_xy_position(fun = "mean_sd", x = "dose") bp + Â stat_pvalue_manual( Â Â Â stat.test, label = "p.adj", tip.length = 0.01, Â Â Â coord.flip = TRUE Â Â Â ) + Â scale_y_continuous(expand = expansion(mult = c(0.05, 0.1))) + Â coord_flip()

Restricted Boltzmann Machine (RBM) »
Conclusion
Using the R function stat pvalue manual() from the ggpubr R package, this article shows how to add p-values to horizontal ggplots.