NColl
NColl

Reputation: 757

Plot pvalue information for mean comparisons by grouping variable

I've put together a plot to view groups separately but now want to include significance levels for mean pairwise comparison in the plot. While I can do the comparison outside of the plot I'm wondering what the most efficient way of including the comparison in the plot would be?

Current Plot

library(tidyverse)

dsub <- diamonds[ sample(nrow(diamonds), 10000), ]

dsub <- dsub %>%
  filter(clarity %in% c('VS2', 'VS1', 'VVS2'))

ggplot(dsub, aes(x = cut, y = carat, fill = clarity)) +
  geom_boxplot(outlier.size = 0) +
  geom_point(pch = 21, position = position_jitterdodge()) 

enter image description here

Now I want to add the comparisons within each level of the cut variable between all levels of the clarity variable. I prefer using ggpubr but couldn't see where this could be achieved.

Upvotes: 3

Views: 224

Answers (2)

Chuck P
Chuck P

Reputation: 3923

EDITED to take OP preference for output into account

Ahhhh... okay well let me at least save you a bunch of vertical space and neaten things up by overcoming the fact that rstatix doesn't honor the order of your factors and ggpubr wants its groups as character not factor.

library(ggplot2)
library(dplyr)

dsub <- diamonds[ sample(nrow(diamonds), 10000), ]
dsub <- dsub %>%
   filter(clarity %in% c('VS2', 'VS1', 'VVS2'))

dsub <- droplevels(dsub)

dsub_stats <- 
   dsub %>%
   group_by(cut) %>%
   rstatix::wilcox_test(carat~clarity) %>% 
   mutate(group1 = factor(group1, 
                          ordered = TRUE, 
                          levels = c("VS2", "VS1", "VVS2"))) %>%
   arrange(cut, group1) %>% 
   mutate(group1 = as.character(group1)) %>%
   rstatix::add_xy_position(x='cut')

ggpubr::ggboxplot(dsub, x = "cut", y = "carat",
                  color = "clarity",
                  add='jitter') +
   ggpubr::stat_pvalue_manual(dsub_stats, 
                              label = "p.adj.signif", 
                              tip.length = 0.01)

Created on 2020-09-24 by the reprex package (v0.3.0)

Upvotes: 5

NColl
NColl

Reputation: 757

library(tidyverse)
library(rstatix)
library(ggpubr)

dsub <- diamonds[ sample(nrow(diamonds), 10000), ]

dsub <- dsub %>%
  filter(clarity %in% c('VS2', 'VS1', 'VVS2'))

dsub_stats <- dsub %>%
  group_by(cut) %>%
  wilcox_test(carat~clarity) %>% add_xy_position(x='cut')


ggboxplot(dsub, x = "cut", y = "carat", 
          color = "clarity", 
          add='jitter'
) +
  stat_pvalue_manual(dsub_stats, label = "p.adj.signif", tip.length = 0.01) 

enter image description here

Upvotes: 1

Related Questions