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

geom_pwc label = "p.adj" breaks with faceting #522

Closed
marklyng opened this issue Nov 30, 2022 · 1 comment
Closed

geom_pwc label = "p.adj" breaks with faceting #522

marklyng opened this issue Nov 30, 2022 · 1 comment

Comments

@marklyng
Copy link

marklyng commented Nov 30, 2022

With non-faceted data, geom_pwc reads label = "p.adj" and appends the adjusted p-value to the plot. When faceting by a variable, label = "p.adj" is ignored and the unadjusted p-value is used instead.

No facet

x <- tibble(value = c(seq(10), seq(10, 100, 10), seq(100, 1000, 100), seq(1000, 10000, 1000)),
            variable = c(rep("a", 10), rep("b", 10), rep("c", 10), rep("d", 10)))


compare_means(value ~ variable, data = x)
# A tibble: 6 × 8
  .y.   group1 group2         p    p.adj p.format p.signif method  
  <chr> <chr>  <chr>      <dbl>    <dbl> <chr>    <chr>    <chr>   
1 value a      b      0.000211  0.00063  0.00021  ***      Wilcoxon
2 value a      c      0.0000108 0.000065 1.1e-05  ****     Wilcoxon
3 value a      d      0.0000108 0.000065 1.1e-05  ****     Wilcoxon
4 value b      c      0.000211  0.00063  0.00021  ***      Wilcoxon
5 value b      d      0.0000108 0.000065 1.1e-05  ****     Wilcoxon
6 value c      d      0.000211  0.00063  0.00021  ***      Wilcoxon

x %>% 
  ggplot(aes(x = variable, y = value)) +
  geom_boxplot() +
  geom_pwc(label = "p.adj")

image

Facet

y <- tibble(value = c(seq(10), seq(10, 100, 10), seq(100, 1000, 100), seq(1000, 10000, 1000)),
            variable = c(rep("a", 10), rep("b", 10), rep("a", 10), rep("b", 10)),
            facet_variable = c(rep("test1", 20), rep("test2", 20)))

compare_means(value ~ variable, data = y, group.by = "facet_variable")
# A tibble: 2 × 9
  facet_variable .y.   group1 group2        p   p.adj p.format p.signif method  
  <chr>          <chr> <chr>  <chr>     <dbl>   <dbl> <chr>    <chr>    <chr>   
1 test1          value a      b      0.000211 0.00042 0.00021  ***      Wilcoxon
2 test2          value a      b      0.000211 0.00042 0.00021  ***      Wilcoxon

y %>% 
  ggplot(aes(x = variable, y = value)) +
  geom_boxplot() +
  geom_pwc(label = "p.adj") +
  facet_grid(. ~ facet_variable)

image

Changing the label to any of the other "p.adj.*" types does not fix the issue.

kassambara added a commit that referenced this issue Dec 4, 2022
kassambara added a commit that referenced this issue Dec 4, 2022
New function ggadjust_pvalue: Adjust p-values Displayed on a GGPlot #522
@kassambara
Copy link
Owner

When using the ggplot facet functions, the p-values are computed and adjusted by panel, without taking into account the other panels. This is by design in ggplot2.

This means that, when there is only one computed p-value by panel, then using label = "p" or label = "p.adj" will give the same results using geom_pwc(). P-value computation and adjustment in a given facet panel is done independently to the other panels.

One might want to adjust the p-values of all the facet panels together. There are two solutions for that:

  1. Using ggadjust_pvalue(p) after creating the plot p. This function is available in the current github dev version, which can be installed using devtools::install_github("kassambara/ggpubr")
  2. or adding the adjusted p-value manually using stat_pvalue_manual(). Read more at:

The following example shows how to use ggadjust_pvalue(p):

suppressPackageStartupMessages(library(ggpubr))

y <- tibble::tibble(value = c(seq(10), seq(10, 100, 10), seq(100, 1000, 100), seq(1000, 10000, 1000)),
            variable = c(rep("a", 10), rep("b", 10), rep("a", 10), rep("b", 10)),
            facet_variable = c(rep("test1", 20), rep("test2", 20)))
p <- y %>% 
  ggplot(aes(x = variable, y = value)) +
  geom_boxplot() +
  geom_pwc(label = "p.adj") +
  facet_grid(. ~ facet_variable)
# Adjust  all panels p-values together
ggadjust_pvalue(p, label = "p.adj")

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