I am looking for help to change the presentation of the p-value on the display in a boxplot.
For now: var = different samples (blood etc.) group: sample.group (positive / negative)..
When running the following code, I get a boxplot with a p-value. The p-value is 6.7e^-07, what I need help doing is:
- Writing p-value with only 3 digits.
- If p<0.001, I want it to write <0.001 on the boxplot.
Can anyone help?
My code is:
library(ggpubr)
fig_boxplot <- function(df, var, group, title = " "){
temp <- aggregate(df[[group]][!is.na(df[[var]])],
by = list(df[[group]][!is.na(df[[var]])]), length)
colnames(temp) <- c("sample.group","n")
temp$new_name <- paste0(temp$sample.group, "\n(n = ", temp$n, ")")
df <- merge(df,
temp[,c("sample.group","new_name")],
by = "sample.group", all.x=F)
my_comparisons <- list( unique(df$new_name))
g1 <- ggplot(data=df, aes(x=df$new_name, y=df[[var]], color=df$new_name)) +
geom_boxplot() +
geom_jitter(width=0.2) +
theme_minimal() +
theme(axis.ticks.x=element_blank(),
axis.text.y = element_text(angle=90, hjust=0.5)) +
labs(title=title) +
stat_compare_means(comparisons = my_comparisons,label = "p.adj",hjust=0) +
theme(plot.subtitle = element_text(hjust=0.5, face="bold")) +
scale_color_manual(values=c("#666666", "#000000")) +
theme(axis.title = element_blank(),
axis.text.y = element_text(angle=0)) +
theme(legend.position = "none") +
coord_flip() +
theme(plot.margin=unit(c(0,0,0,0), "mm"))
return(g1)
}
fig_boxplot(df, var = "CSF.WBC.RBC.ratio", group, title = "A) CSF-WBC / CSF-RBC")