Valérie
Valérie

Reputation: 21

stat_pvalue_manually and stat_compare_means with Tukey HSD

I am using ggpubr and ggplot2. First I did a anova test on my 3 clusters and now I would like to include my p-adj values of the TukeyHSD test in my box plot by using

stat_compare_means or stat_pvalue_manually

since I found out that Tukey test cannot be shown with stat_compare_means unfortunately..

Thanks

Upvotes: 2

Views: 1416

Answers (2)

Kra.P
Kra.P

Reputation: 15153

You may try

library(ggplot2)

library(ggpubr)
library(tidyverse)

library(rstatix)

tukey <- diamonds %>%
  tukey_hsd(price ~ cut) %>%
  add_significance() %>%
  add_xy_position() %>%
  filter(group1 == "Fair")

diamonds %>%
  ggplot(aes(x = fct_rev(cut), y = price)) +
  geom_boxplot() +
  stat_pvalue_manual(tukey,
                     hide.ns = T,
                     label = "p.adj")

enter image description here

Upvotes: 0

Shawn Hemelstrand
Shawn Hemelstrand

Reputation: 3258

This was asked a long time ago but in case somebody comes across it, here is an example with the iris dataset since there was none shown in your question. First, load the three libraries shown. Then fit the ANOVA in the next code chunk, fit the Tukey test with additional p value/xy positions in the data, then add them as labels like so:

#### Load Libraries ####
library(tidyverse)
library(rstatix)
library(ggpubr)

#### Fit Data ####
fit <- iris %>% 
  anova_test(Sepal.Length ~ Species) %>% 
  add_significance()

#### Run Tukey ###
tukey <- iris %>% 
  tukey_hsd(Sepal.Length ~ Species) %>% 
  add_significance() %>% 
  add_xy_position()

#### Plot ####
ggboxplot(iris,
          x="Species",
          y="Sepal.Length")+
  stat_pvalue_manual(tukey,
                     hide.ns = T)+
  labs(subtitle = get_test_label(fit,
                            detailed = TRUE),
  caption = get_pwc_label(tukey))

It should turn out like this:

enter image description here

Upvotes: 1

Related Questions