Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support statistical annotation for grouped plots #4

Closed
LeiGuo0812 opened this issue Nov 19, 2024 · 4 comments
Closed

Support statistical annotation for grouped plots #4

LeiGuo0812 opened this issue Nov 19, 2024 · 4 comments

Comments

@LeiGuo0812
Copy link

Hi,
I sincerely appreciate the tidyplots package. It's a fantastic tool for creating beautiful and tidy plots efficiently.

I particularly like the stat_pvalue_manual function in the ggpubr package, which is great for handling complex grouped or faceted plots. However, it seems that the current add_test_pvalue function in tidyplots only supports statistical tests for a single factor.

I tried using the add function to directly apply the stat_pvalue_manual, but encountered some errors. Is it possible to enhance tidyplots to support more complex grouped or faceted plots? Or is there currently a workaround for these scenarios?

Below are the test codes I used:

library(ggpubr)
library(rstatix)
library(tidyplots)

df <- ToothGrowth
df$dose <- as.factor(df$dose)
head(df, 3)

stat.test <- stat.test <- df %>%
  group_by(dose) %>%
  t_test(len ~ supp) %>%
  add_xy_position(x = "dose", dodge = 0.8)
  
stat.test2 <- df %>%
  t_test(len ~ dose) %>% 
  add_xy_position(x = "dose")

stat.test3 <- df %>%
  group_by(supp) |> 
  t_test(len ~ dose) %>% 
  add_xy_position(x = "dose", group = 'supp')

# it worked fine for ggplot
ggplot(df, aes(x = dose, y = len, color = supp)) +
  geom_boxplot() +
  stat_pvalue_manual(
    stat.test,  label = "p", tip.length = 0
  ) +
  stat_pvalue_manual(
    stat.test2,  label = "p", tip.length = 0.02,
    step.increase = 0.05
  ) +
  stat_pvalue_manual(
    stat.test3,  label = "p", tip.length = 0.02,
    step.increase = 0.05, step.group.by = 'supp'
  )

# but get error when using tidyplots
tidyplot(df, x = dose, y = len, color = supp)  |> 
  add_boxplot() |> 
  add(add_pvalue(
    stat.test,  label = "p", tip.length = 0
  ))  |>
  add(stat_pvalue_manual(
    stat.test2,  label = "p", tip.length = 0.02,
    step.increase = 0.05
  ))  |>
  add(stat_pvalue_manual(
    stat.test3,  label = "p", tip.length = 0.02,
    step.increase = 0.05, step.group.by = 'supp'
  ))

#Error:
#! Problem while computing aesthetics.
#ℹ Error occurred in the 2nd layer.
#Caused by error:
#! can not find object 'supp'

Thank you so much for your hard work on this amazing package, and I really look forward to your response!

@jbengler
Copy link
Owner

jbengler commented Nov 19, 2024

Hi @LeiGuo0812

Thank you for using tidyplots!

Your error occurs because ggpubr::stat_pvalue_manual() fails when both color and fill aesthetics are provided.
I can replicate the issue in your ggplot2 code by adding fill = supp and fix it in tidyplots by setting fill = NA. See the example below.

A bit of background: tidyplots automatically assigns the same variable to both color and fill unless explicitly specified otherwise.

Best
Jan

# adding aes(fill = supp) breaks the ggplot
# does not work
ggplot(df, aes(x = dose, y = len, color = supp, fill = supp)) +
  geom_boxplot() +
  stat_pvalue_manual(
    stat.test,  label = "p", tip.length = 0
  ) +
  stat_pvalue_manual(
    stat.test2,  label = "p", tip.length = 0.02,
    step.increase = 0.05
  ) +
  stat_pvalue_manual(
    stat.test3,  label = "p", tip.length = 0.02,
    step.increase = 0.05, step.group.by = 'supp'
  )

# adding aes(fill = NA) rescues the tidyplot
# just works
tidyplot(df, x = dose, y = len, color = supp, fill = NA) %>% 
  add_boxplot() %>% 
  add(stat_pvalue_manual(
    stat.test,  label = "p", tip.length = 0
  )) %>%
  add(stat_pvalue_manual(
    stat.test2,  label = "p", tip.length = 0.02,
    step.increase = 0.05
  )) %>% 
  add(stat_pvalue_manual(
    stat.test3,  label = "p", tip.length = 0.02,
    step.increase = 0.05, step.group.by = 'supp'
  ))

@LeiGuo0812
Copy link
Author

Dear @jbengler
Thank you very much for your quick response! It perfectly solved my problem.
I have another question: if I need to specify both color and fill mapping in ggplot2, I can avoid the error by moving the mapping down to the sub-layers. I wonder if there is a similar way to handle this in tidyplots?

@jbengler
Copy link
Owner

Unfortunately not. In an attempt to keep things simple, the mappings in tidyplots are restricted to the tidyplot() call.

Since the error also occurs in plain ggplot2 + ggpubr, one could report this issue in ggpubr. Maybe they are able to fix it for all ggpubr users.

@jbengler
Copy link
Owner

I opened this ggpubr issue kassambara/ggpubr#621

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants