P-values can be added to ggplot2
figures using the function ggpubr::stat_compare_mean()
. However I cannot get the text "p = " to show up in front of the p-values. There are examples of how to add "p = " in front of p-values on the help page for the function but they do not seem to work.
Example
library(ggplot2)
library(ggpubr)
library(dplyr)
data("Cars93")
# List of the comparisons I would like to make for which p-values will be derived
my_comparisons <- list(c("Front", "Rear"),
c("Front", "4WD"),
c("Rear", "4WD"))
# creates the figure with p-value but no label indicating the values are p-values
Cars93 %>%
mutate(DriveTrain = factor(DriveTrain, levels = c("Front","Rear","4WD"))) %>%
ggplot(aes(x = DriveTrain, y = Price))
stat_compare_means(paired = F,
comparisons = my_comparisons)
geom_boxplot(outlier.colour="white", outlier.fill = "white", outlier.shape = 1, outlier.size = 0)
geom_jitter(shape=1, position=position_jitter(0.2), color = "black", fill = "white", size = 2)
theme_bw()
theme(axis.text.x = element_text(size = 16, color = "black"),
axis.text.y = element_text(size = 16, color = "black"),
axis.title = element_text(size = 16, color = "black"),
axis.title.x = element_text(vjust = -0.5),
panel.grid = element_blank(),
panel.background = element_blank())
following the example at the bottom of the ?stat_compare_means
page suggests using aes(label = paste0("p = ", ..p.format..)
which does not work.
?stat_compare_means
Cars93 %>%
mutate(DriveTrain = factor(DriveTrain, levels = c("Front","Rear","4WD"))) %>%
ggplot(aes(x = DriveTrain, y = Price))
stat_compare_means(paired = F,
comparisons = my_comparisons,
aes(label = paste0("p = ", ..p.format..)))
geom_boxplot(outlier.colour="white", outlier.fill = "white", outlier.shape = 1, outlier.size = 0)
geom_jitter(shape=1, position=position_jitter(0.2), color = "black", fill = "white", size = 2)
theme_bw()
theme(axis.text.x = element_text(size = 16, color = "black"),
axis.text.y = element_text(size = 16, color = "black"),
axis.title = element_text(size = 16, color = "black"),
axis.title.x = element_text(vjust = -0.5),
panel.grid = element_blank(),
panel.background = element_blank())
If you look at the label
argument on the ?stat_compare_means
help page it says the allowed values include "p.signif"
or "p.format"
which made me think ..p.format..
was deprecated, so I tried adding in "p.format"
which also did not work.
Cars93 %>%
mutate(DriveTrain = factor(DriveTrain, levels = c("Front","Rear","4WD"))) %>%
ggplot(aes(x = DriveTrain, y = Price))
stat_compare_means(paired = F,
comparisons = my_comparisons,
aes(label = paste0("p = ", "p.format")))
geom_boxplot(outlier.colour="white", outlier.fill = "white", outlier.shape = 1, outlier.size = 0)
geom_jitter(shape=1, position=position_jitter(0.2), color = "black", fill = "white", size = 2)
theme_bw()
theme(axis.text.x = element_text(size = 16, color = "black"),
axis.text.y = element_text(size = 16, color = "black"),
axis.title = element_text(size = 16, color = "black"),
axis.title.x = element_text(vjust = -0.5),
panel.grid = element_blank(),
panel.background = element_blank())
In the end I would like the p-values to be preceded by p =
such that the labels would say p = 0.00031, p = 0.059, and p = 0.027.
CodePudding user response:
When you use a list of comparisons, stat_compare_means
defaults to using geom_signif
from the ggsignif
package, essentially acting as a glorified wrapper function. In so doing, you lose some of the formatting flexibility. Better in this case to use geom_signif
directly:
library(ggsignif)
Cars93 %>%
mutate(DriveTrain = factor(DriveTrain, levels = c("Front","Rear","4WD"))) %>%
ggplot(aes(x = DriveTrain, y = Price))
geom_signif(y_position = c(55, 60, 65),
comparisons = my_comparisons,
map_signif_level = function(x) paste("p =", scales::pvalue(x)))
geom_boxplot(outlier.colour="white", outlier.fill = "white",
outlier.shape = 1, outlier.size = 0)
geom_jitter(shape=1, position=position_jitter(0.2),
color = "black", fill = "white", size = 2)
theme_bw()
theme(axis.text.x = element_text(size = 16, color = "black"),
axis.text.y = element_text(size = 16, color = "black"),
axis.title = element_text(size = 16, color = "black"),
axis.title.x = element_text(vjust = -0.5),
panel.grid = element_blank(),
panel.background = element_blank())