Reputation: 428
Hi I have a sample df as follows and I'm trying to perform t tests using the ggpubr
package with the stat_compare_means()
function however I cannot get it to compare across the groups that I want:
set.seed(13)
df = data.frame(grouping = c(rep("A",24),rep("B",24),rep("C",24)),
id = c(rep(1,12),rep(2,12),rep(3,12),rep(4,12),rep(5,12),rep(6,12)),
subject = c(rep(c("One","Two","Three","Four"),72)),
family = c(rep(c("Red","Red","Red","Red","Blue","Blue","Blue","Blue","Green","Green","Green","Green"),6)),
percent = sample(1:100,288,replace=T),
result = "Good")
df %>%
ggplot(aes(x=subject,y=percent,group = interaction(subject,grouping))) +
geom_boxplot(outlier.shape = NA) +
geom_point(aes(color = grouping),size=1.75,position = position_dodge(width = 0.75))+
scale_y_continuous(breaks = seq(0, 100, by = 25),limits = c(0,103)) +
facet_grid(result~family,switch = "y") +
scale_x_discrete("") +
theme_minimal() +
stat_compare_means(aes(group = grouping),label="p.signif",
method = "t.test",paired=F,
tip.length=0,step.increase = 0.05)
As seen from the picture it only has one "ns" for the statistical test but is it possible to have it for all three comparisons? I would want the tests to be between the grouping variable of A, B, and C so for example within the 3 boxplots of "Four" there would be 3 comparisons for A vs B, A vs C, and B vs C. I also tried adding comparisons = list(c("A","B"),c("A","C"),c("B","C"))
as the comparisons
argument but it also didn't work. Thanks a lot!
Upvotes: 4
Views: 3305
Reputation: 41225
Edit
The key thing here is that the comparisons are based on:
comparisons: A list of length-2 vectors. The entries in the vector are either the names of 2 values on the x-axis or the 2 integers that correspond to the index of the groups of interest, to be compared.
So you have to use interaction
on the x-axis to be able to compare the boxplots per grouping. Here is a reproducible example:
library(ggplot2)
library(ggpubr)
library(dplyr)
df %>%
ggplot(aes(x=interaction(subject, grouping),y=percent)) +
geom_boxplot(aes(group = interaction(subject, grouping)), outlier.shape = NA) +
geom_point(aes(color = grouping, group = grouping),size=1.75,position = position_dodge(width = 0.75))+
scale_y_continuous(breaks = seq(0, 100, by = 25)) +
scale_x_discrete("", limits = c("Four.A", "Four.B", "Four.C", "One.A", "One.B", "One.C", "Three.A", "Three.B", "Three.C", "Two.A", "Two.B", "Two.C"),
labels = c(rep(c("", "Four", "", "", "One", "", "", "Three", "", "", "Two", ""), 3))) +
stat_compare_means(comparisons = list(c("Four.A","Four.B"),c("Four.A","Four.C"),c("Four.B","Four.C"),
c("One.A","One.B"),c("One.A","One.C"),c("One.B","One.C"),
c("Three.A","Three.B"),c("Three.A","Three.C"),c("Three.B","Three.C"),
c("Two.A","Two.B"),c("Two.A","Two.C"),c("Two.B","Two.C")),
method = "t.test",
paired=F,
tip.length=0,step.increase = 0.05) +
facet_wrap(~ family) +
theme_minimal()
Created on 2022-08-31 with reprex v2.0.2
Old answer
Maybe you want something like this. Also make sure to remove the limits
in your scale_y_continuous
, otherwise the labels won't be shown. Here is a reproducible example:
set.seed(13)
df = data.frame(grouping = c(rep("A",24),rep("B",24),rep("C",24)),
id = c(rep(1,12),rep(2,12),rep(3,12),rep(4,12),rep(5,12),rep(6,12)),
subject = c(rep(c("One","Two","Three","Four"),72)),
family = c(rep(c("Red","Red","Red","Red","Blue","Blue","Blue","Blue","Green","Green","Green","Green"),6)),
percent = sample(1:100,288,replace=T),
result = "Good")
library(ggplot2)
library(ggpubr)
library(dplyr)
df %>%
ggplot(aes(x=subject,y=percent, group = interaction(subject,grouping))) +
geom_boxplot(outlier.shape = NA) +
geom_point(aes(color = grouping, group = grouping),size=1.75,position = position_dodge(width = 0.75))+
scale_y_continuous(breaks = seq(0, 100, by = 25)) +
facet_wrap(~ family) +
scale_x_discrete("") +
theme_minimal() +
stat_compare_means(comparisons = list(c("Four", "One"), c("Four", "Three"), c("Four", "Two"), c("One", "Three"), c("One", "Two"), c("Three", "Two")),
aes(label=..p.adj..),
method = "t.test",paired=F,
tip.length=0,step.increase = 0.05)
#> Warning: Using `as.character()` on a quosure is deprecated as of rlang 0.3.0.
#> Please use `as_label()` or `as_name()` instead.
#> This warning is displayed once per session.
Created on 2022-08-28 with reprex v2.0.2
Upvotes: 5