Home > Blockchain >  stat_compare_mean() does not work on ggboxplot() with multiple y values
stat_compare_mean() does not work on ggboxplot() with multiple y values

Time:01-30

I am trying to add p-values to my boxplot using ggboxplpot, but it seems stat_compare_means() doesn't work when I have multiple y = values.

here is the sample code from palmerpenguin dataset

library(palmerpenguins) 
library(tidyverse) 
library(ggplot2)
library(ggpubr)

#Load data
data(package = 'palmerpenguins')

#Remove NA data
df_clean <- na.omit(penguins)

#Group dataset according to species
df_new <- df_clean %>% 
  group_by(species)

#Generate multiple boxplots
df_boxplot <- ggboxplot(df_new, 
                  x = "species", 
                  y = c("bill_length_mm", "bill_depth_mm", "flipper_length_mm", "body_mass_g"),
                  ylab = "Bill Length (mm)", 
                  xlab = "Species", 
                  color = "species", 
                  fill = "species", 
                  notch = TRUE, 
                  alpha = 0.5, 
                  ggtheme = theme_pubr())  
                  stat_compare_means()
df_boxplot

I also tried adding a comparison list but it didn't worked

I added this variable:

comp_list <- list(c("Chinstrap", "Adelie"), c("Chinstrap", "Gentoo"), c("Adelie", "Gentoo"))

then change stat_compare_nea() to stat_compare_nea(comparison = comp_list)

I hope someone can provide an alternative and explain why this does not work. Why won't stat_compare_mean() won't automatically add p values to the 4 different boxplots being created in df_boxplot

CodePudding user response:

The issue is that ggboxplot returns a list of ggplots, one for each of your variables. Hence adding stat_compare_means() to list won't work but instead will return NULL.

To add p-values to each of your plots have to add stat_compare_means() to each element of the list using e.g. lapply:

library(palmerpenguins)
library(tidyverse)
library(ggplot2)
library(ggpubr)

# Remove NA data
df_clean <- na.omit(penguins)

# Group dataset according to species
df_new <- df_clean %>%
  group_by(species)

# Generate multiple boxplots
df_boxplot <- ggboxplot(df_new,
  x = "species",
  y = c("bill_length_mm", "bill_depth_mm", "flipper_length_mm", "body_mass_g"),
  ylab = "Bill Length (mm)",
  xlab = "Species",
  color = "species",
  fill = "species",
  notch = TRUE,
  alpha = 0.5,
  ggtheme = theme_pubr()
)

lapply(df_boxplot, function(x) x   stat_compare_means())
#> $bill_length_mm

#> 
#> $bill_depth_mm

  •  Tags:  
  • Related