Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot pvalue information for mean comparisons by grouping variable

Tags:

r

ggplot2

ggpubr

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.

like image 886
NColl Avatar asked Jun 22 '26 09:06

NColl


1 Answers

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)

like image 125
Chuck P Avatar answered Jun 25 '26 00:06

Chuck P



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!