kassambara / ggpubr

'ggplot2' Based Publication Ready Plots
https://rpkgs.datanovia.com/ggpubr/
1.14k stars 165 forks source link

Displaying all pairwise comparisons in stat_compare_means ? #355

Open courtiol opened 4 years ago

courtiol commented 4 years ago

Although it is not always a good thing to do, it could be useful to be able to display all pairwise comparisons at once. As far as I know, if one does not want to indicate all the comparison, one need to do something like that:

library(tidyverse)
library(ggpubr)

ttests <- compare_means(Sepal.Length ~ Species, data = iris, method = "t.test")

extract_all_comparisons <- function(.tbl) {
  .tbl %>%
    dplyr::select(.data$group1, .data$group2) %>%
    purrr::transpose() %>%
    purrr::modify_depth(1, unlist)
}

iris %>%
  ggplot() +
  aes(y = Sepal.Length, x = Species) +
  geom_boxplot(width = 0.2) +
  geom_jitter(alpha = 0.2, width = 0.05) +
  stat_compare_means(comparisons = extract_all_comparisons(ttests), method = "t.test")

Created on 2020-11-16 by the reprex package (v0.3.0)

This is a little awkward since compare_means must be run twice and the user must be able to create the list (or as above some function creating the list).

Perhaps such a thing could be integrated within stat_compare_means() and available to the user via the comparisons argument which could recognize something special (e.g. "ALL" or TRUE)?

grt9 commented 1 year ago

+1 for this feature