Broken t-test in facet_wrap

371 views Asked by At

I am trying to use, at the same time, facet_wrap and stat_compare_means, but I have a problem. The two sides of the data do not have the same number of points. Therefore the stat_compare_means fails... Look for instance at the image:

enter image description here

in Type1 "B" there are three points, whereas in Type2 "B" there is only one point. This discrepancy makes that almost all t-test fails and are not plotted. What I need is the t-test for the groups that have a matching number of points (in this case all the t-test on Type1, and A vs C in Type2). The plot used is the following:

library(RColorBrewer)
library(ggpubr)
library(BBmisc)


adf=read.csv("test1.txt", sep=" ")

myColors <- brewer.pal(length(unique(adf$ID)) ,"Set1")
names(myColors) <- unique(adf$ID)
colScale <- scale_colour_manual(name = "ID",values = myColors)

my_comparison=as.data.frame(combn(unique(adf$sampletype) ,2))
my_comparison=convertColsToList(my_comparison)

ggplot(adf, aes(x=sampletype, y=value, fill=sampletype ))+
  geom_point(aes(group=ID, colour=ID))+
  geom_line(aes(group=ID, colour=ID))+
  facet_wrap(~response, scale="free")+
  colScale+
  ggtitle("Entropy")+
  theme(text = element_text(size=20))+
  stat_compare_means(comparisons = my_comparison, method = "t.test", paired = TRUE)

The data (saved as test1.txt):

sampletype value ID response
A 8.192 gr_6 Type2
B 13.99 gr_6 Type2
C 9.186 gr_6 Type2
A 5.616 gr_5 Type1
B 15.55 gr_5 Type1
C 7.126 gr_5 Type1
A 5.484 gr_4 Type1
B 12.54 gr_4 Type1
C 4.492 gr_4 Type1
A 9.949 gr_3 Type2
C 6.631 gr_3 Type2
A 2.533 gr_7 Type2
C 12.25 gr_7 Type2
A 2.196 gr_2 Type2
C 6.447 gr_2 Type2
A 11.20 gr_1 Type1
B 16.63 gr_1 Type1
C 6.637 gr_1 Type1

Is there a workaround?

2

There are 2 answers

1
NColl On BEST ANSWER

I have a way of doing it but the data provided failed a t-test due to lack of variance so I did alter it.

library(tidyverse)
library(rstatix)
library(ggpubr)
df <- enframe(c("A 8.192 gr_6 Type2",
"B 13.99 gr_6 Type2",
"C 9.186 gr_6 Type2",
"A 5.616 gr_5 Type1",
"B 15.55 gr_5 Type1",
"C 7.126 gr_5 Type1",
"A 5.484 gr_4 Type1",
"B 12.54 gr_4 Type1",
"C 4.492 gr_4 Type1",
"A 9.949 gr_3 Type2",
"C 6.631 gr_3 Type2",
"A 2.533 gr_7 Type2",
"C 12.25 gr_7 Type2",
"A 2.196 gr_2 Type2",
"C 6.447 gr_2 Type2",
"A 11.20 gr_1 Type1",
"B 16.63 gr_1 Type1",
"C 6.637 gr_1 Type1"))
df <- df %>%
  separate(value, into =c("sampletype", "value", "ID", "response"), 
           sep=" ") %>% select(-name) %>%
  mutate(
    value = sample(1:50, 18)
  )

keep_vars <- df %>%
  group_by(response, sampletype) %>% tally() %>% filter(n>1) %>% 
  pivot_wider(names_from = sampletype, values_from = n) %>%split(.$response) %>%
  map(pivot_longer, cols=-c(response)) %>% map(filter,value>=0) %>% bind_rows(.) %>%
  mutate(UID = paste0(response, name)) %>%pull(UID)
  
df_plot <- df %>% mutate(UID = paste0(response, sampletype))  %>%
  filter(UID %in% keep_vars) %>%
  group_by(response) %>%
  t_test(value~sampletype) %>% add_xy_position(x='sampletype')

ggpubr::ggline(df, 
                  x='sampletype', 
                  y='value',
                  color = 'ID',
                  add='jitter',
                  facet.by = 'response'
                  ) +
  stat_pvalue_manual(df_plot, 
                     label = "p.adj.signif", 
                     tip.length = 0.01)

enter image description here

0
Fabrizio On

The solution of NColl is great, I have also endorsed it but for my specific case, it failed. In particular when there is a single value for one of the cases (like a single value at Type2, A, gr_X). This is my fault, the example lacks the generality I wanted. Nonetheless, Ncoll makes me realize that I have to look at the variables I want to compare. I didn't want to keep bothering so I implemented my solution. In practise, I plot two ggplot and I merge them with gridExtra:

adf_r=adf[adf$sampletype %in% ctime  & adf$response=="Responders", ]
adf_nr=adf[adf$sampletype %in% ctime &d adf$response=="Non-responders", ]

adrmyColors <- brewer.pal(length(unique(adf$ID)) ,"Set1")
names(myColors) <- unique(adf$ID)
colScale <- scale_colour_manual(name = "ID",values = myColors)


for(st in unique(adf_r$sampletype)){
  if(sum(adf_r$sampletype==st)>=3){
    keepvar1=c(keepvar1,st)
  }
}

my_comparison=NULL    
if(length(keepvar1)>1){
  my_comparison=as.data.frame(combn(keepvar1,2))
  my_comparison=convertColsToList(my_comparison)
}

p1=ggplot(adf_r, aes(x=sampletype, y=value, fill=sampletype ))+
  geom_point(aes(group=ID, colour=ID))+
  geom_line(aes(group=ID, colour=ID))+
   colScale+
   ggtitle("Entropy")+
   theme(text = element_text(size=20))+
   guides(fill=FALSE)+
   stat_compare_means(comparisons = my_comparison, method = "t.test", paired = TRUE)


keepvar2=NULL
for(st in unique(adf_nr$sampletype)){
  if(sum(adf_nr$sampletype==st)>=3){
    keepvar2=c(keepvar2,st)
  }
}

my_comparison=NULL
if(length(keepvar2)>1){
  my_comparison=as.data.frame(combn(keepvar2,2))
  my_comparison=convertColsToList(my_comparison)
}

p2=ggplot(adf_nr, aes(x=sampletype, y=value, fill=sampletype ))+
  geom_point(aes(group=ID, colour=ID))+
  geom_line(aes(group=ID, colour=ID))+
  colScale+
  ggtitle("Entropy")+
  guides(fill=FALSE)+
  theme(text = element_text(size=20))+
  stat_compare_means(comparisons = my_comparison, method = "t.test", paired = TRUE)

final_p=gridExtra::grid.arrange(p1, p2, ncol = 2)