stat_compare_means function - Error 'must have same length'
Entering edit mode
Elif • 0
Last seen 3 months ago
Puerto Rico


We are trying to compute a Kruskal-Wallis test on diversity indices (Chao1, Shannon) through the following code using the function stat_compare_means (that by default uses wilkoxon) from ggpubr.


my_comparisons <- list(c("gut C. decolorata female","larva"), c("larva","gut C. decolorata male"), c("gut C. decolorata female", "gut C. decolorata male"), c("gut C. decolorata male", "pollen provision"), c("gut C. decolorata female", "pollen provision"), c("larva", "pollen provision"))

p <- plot_richness(physeq, x = "sample_type", measures = c("Chao1", "Shannon"), scales="free_y")

p+ geom_boxplot(aes(fill = sample_type), alpha=0.8)+ guides(fill = guide_legend(title = "Sample type of Centris decolorata and pollen provision"))+
  scale_fill_manual(values = color_cdpp, labels=c("gut C. decolorata female", "gut C. decolorata male", "larva", "pollen provision"))+
  scale_x_discrete(labels=c("gut C. decolorata female", "gut C. decolorata male", "larva", "pollen provision"))+
  xlab("Sample type")+
  theme(axis.title.x =element_text(size=15), axis.title.y =element_text(size=15), axis.text.y = element_text(size=15),
        legend.text = element_text(size=15), 
        strip.text = element_text(size=15),
        legend.title=element_markdown(size=15), axis.text.x = element_text(size=15), 
        legend.justification = "center",
        legend.text.align = 0) -> p1

p1 + stat_compare_means(comparisons = my_comparisons, label="p.signif", method = 'kruskal.test',
                        hide.ns = FALSE,
                        symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1), 
                                           symbols = c("*", "*", "", "", "ns")))

We receive the following error:

Warning messages:

1: Computation failed in stat_signif():
'x' and 'g' must have the same length
2: Computation failed in stat_signif():
'x' and 'g' must have the same length

We would greatly appreciate any insight on to why we receive this error message and how we could fix it.

microbiomeDataSets Microbiome • 378 views
Entering edit mode

Have you tried to replace method by test? stat_compare_means(comparisons = my_comparisons, label="p.signif", test = 'kruskal.test' ...)

Entering edit mode

Hello Basti,

Thanks for suggesting this. I've tried it (I saw that in StackoverFlow I think), but it seems the value of the argument test = "kruskal.test" is not recognized by R. So even though R doesn't give any error after this argument, it seems it does nott recognize it and just does a Wilcoxon test (it gives the same values as when using method = "wilcox.test").


Login before adding your answer.

Traffic: 208 users visited in the last hour
Help About
Access RSS

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6