Is there a way to name the values of the t.test scored for each 2 groups

I have already named the values of the t.test scores in the plot by giving dimensions to my_comparisions. Is there an alternate way to do this. Because, I am using this inside R shiny and hence there are lots of columns (similar to ColB) involved. So want to know if there is any other method

library(ggpubr)
library(ggplot2)
df <- structure(list(ColA = c(39, 13, 16, 36, 20, 18, 23, 21, 47, 46, 
15, 11, 13, 36, 48, 18, 31, 32, 19, 39, 40, 41, 45, 38, 18, 38, 
38), ColB = c("A", "A", "A", "A", "A", "A", "A", "A", "A", "B", 
"B", "B", "B", "B", "B", "B", "C", "C", "C", "D", "D", "D", "D", 
"D", "D", "D", "D")), row.names = c(NA, -27L), class = c("tbl_df", 
"tbl", "data.frame"))
df <- as.data.frame(df)
my_comparisons <- list( c("A", "B"), c("A", "C"), c("A", "D"), c("B", "C"), c("B", "D"), c("C", "D"))
ggboxplot(df ,x = "ColB", y = "ColA",
          color = "ColB", palette = "jco",
          add = "jitter")+stat_compare_means(comparisons = my_comparisons)

Are you looking for something like this?

Combinations <- combn(LETTERS[1:4], m = 2, simplify = FALSE)
Combinations
#> [[1]]
#> [1] "A" "B"
#> 
#> [[2]]
#> [1] "A" "C"
#> 
#> [[3]]
#> [1] "A" "D"
#> 
#> [[4]]
#> [1] "B" "C"
#> 
#> [[5]]
#> [1] "B" "D"
#> 
#> [[6]]
#> [1] "C" "D"

Created on 2019-10-17 by the reprex package (v0.3.0.9000)

1 Like

Thanks for your effort. Even I thought of this. But let us think in this way, in ColB the levels are A, B, C and D right? so we are doing this. But let us say there is another Column with different levels. My thinking is we should not keeping on writing this code again. When I select, particular column, the plot should be displayed with respective t scores

FJCC has already given you a clever solution, you just have to apply it to your code, maybe this example would make it clearer for you.

df <- structure(list(ColA = c(39, 13, 16, 36, 20, 18, 23, 21, 47, 46, 
                              15, 11, 13, 36, 48, 18, 31, 32, 19, 39, 40, 41, 45, 38, 18, 38, 
                              38), ColB = c("A", "A", "A", "A", "A", "A", "A", "A", "A", "B", 
                                            "B", "B", "B", "B", "B", "B", "C", "C", "C", "D", "D", "D", "D", 
                                            "D", "D", "D", "D")), row.names = c(NA, -27L), class = c("tbl_df", 
                                                                                                     "tbl", "data.frame"))
# This simulates shiny dynamic input
input <- NULL
input$b <- "ColB"

# This would reactively generate the list
my_comparisons <- combn(unique(df[[input$b]]), m = 2, simplify = FALSE)
my_comparisons
#> [[1]]
#> [1] "A" "B"
#> 
#> [[2]]
#> [1] "A" "C"
#> 
#> [[3]]
#> [1] "A" "D"
#> 
#> [[4]]
#> [1] "B" "C"
#> 
#> [[5]]
#> [1] "B" "D"
#> 
#> [[6]]
#> [1] "C" "D"
1 Like

yeah, Actually I altered his solution in my code, and it is working. Thanks a lot

So you want to select one column and do the comparison (show the t-test) for the other conditions against this one?

Like this: http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/76-add-p-values-and-significance-levels-to-ggplots/#compare-more-than-two-groups
(scroll down to Multiple pairwise tests against a reference group )

1 Like

Perfect. Thanks a lot.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.