Home > Software design >  How to display p-values above boxplots on exponential (log10) y-axis?
How to display p-values above boxplots on exponential (log10) y-axis?

Time:09-07

I have a data frame with three groups (group1, group2, group3). I would like to show the p-value of their mean comparisons in ggplot2 which I can do however, the values are stacked ontop of one another making it difficult to see what is being compared. When I try to adjust where the p-values are located using the y_position() function, the boxplots collapse (I think because the y-axis is log10) but the p-values are no longer stacked ontop of one another. How can I keep the boxplots from collapsing and keep the p-values displayed so that you can see what is being compared?

Example data

library(ggplot2)
library(dplyr)
library(ggsignif)

df <- data.frame(matrix(ncol = 2, nrow = 30))
colnames(df)[1:2] <- c("group", "value")
df$group <- rep(c("group1","group2","group3"), each = 10)
df[1:10,2] <- rexp(10, 1/10)
df[11:20,2] <- rexp(10, 1/100)
df[21:30,2] <- rexp(10, 1/900)

# Need to say what should be compared for p-value determination

my_comparisons <- list(c("group1", "group2"),
                       c("group1", "group3"),
                       c("group2", "group3"))

Boxplots showing the distribution of value for each group however the p-values are ontop of one another so you cannot compare among groups.

df %>%
  mutate(group = factor(group, levels = c("group3","group2","group1"))) %>%
  ggplot(aes(x = group, y = value))  
  geom_signif(comparisons = my_comparisons,
              map_signif_level = function(x) paste("p =", scales::pvalue(x)))  
  scale_y_log10()  
  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)  
  labs(x = "",
       y = "value")  
  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())

Adjusting the y_position() of where the p-values should display but this collapses the y-axis. I have tried several values within y_position.

df %>%
  mutate(group = factor(group, levels = c("group3","group2","group1"))) %>%
  ggplot(aes(x = group, y = value))  
  geom_signif(y_position = c(2000,1800,1600),
              comparisons = my_comparisons,
              map_signif_level = function(x) paste("p =", scales::pvalue(x)))  
  scale_y_log10()  
  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)  
  labs(x = "",
       y = "value")  
  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())

CodePudding user response:

For some reason this parameter ignores the axis transformation. You therefore need to use the log10 values of the desired positions:


df %>%
  mutate(group = factor(group, levels = c("group3","group2","group1"))) %>%
  ggplot(aes(x = group, y = value))  
  geom_signif(comparisons = my_comparisons,
              y_position = log10(c(5000, 10000, 25000)),
              map_signif_level = function(x) paste("p =", scales::pvalue(x)))  
  scale_y_log10()  
  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)  
  labs(x = "",
       y = "value")  
  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())

enter image description here

  • Related