How to insert lines in the graphs to denote post-hoc differences between compared groups?

Asked

Viewed 190 times

2

I need to graphically identify post-hoc differences between mean comparisons. As the figure below:

Percebam as linhas em cima dos box-plots e asteriscos que denotam a significância

Send a dput with simple data to facilitate graph generation (not the figure), as well as the chart base to be continued.

> dput(df)
structure(list(Grupo = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("A", "B", "C"), class = "factor"), 
    value = c(1.14901252600116, 2.2759735134712, 1.65553164019443, 
    1.56369896215724, 2.72398274895494, 2.48108360073242, 2.26546377144311, 
    2.33209770776873, 2.28731709992939, 1.81524976926264, 3.09226862162988, 
    3.79822202874918, 3.71669618296597, 4.00204796633077, 4.47209295173977, 
    4.33828240733588, 3.83323798255772, 4.20237841036556, 4.27585406310512, 
    4.01484026647116, 7.7037558305269, 7.79523829060191, 8.44198516477667, 
    8.76460991202211, 8.92519252259177, 7.66958621885233, 7.38649721403375, 
    7.29882998824104, 7.33562774857289, 7.48731319375855)), row.names = c(NA, 
-30L), class = "data.frame")


pl <- ggplot(df,aes(x = Grupo, y = value, fill = Grupo)) + geom_boxplot()

I am aware that the graphpad Prism software runs this with some ease. But, as everyone knows it has a high cost and therefore, I believe that in R is possible, even less intuitively than software click to the.

Therefore, it is possible to insert these lines with some basic function or some specific package is needed?

  • 2

    The ggpubr package does what it needs. Check the documentation (in English): https://rpkgs.datanovia.com/ggpubr/index.html

2 answers

5


As it is in the comment of Carlos Eduardo Lagosta, the package ggpubr has a function, stat_compare_means which can insert the comparison lines. In the case of the example below, these comparisons will be those made by t tests, function t.test.

library(ggplot2)
library(ggpubr)

symnum.args <- list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1), 
                    symbols = c("****", "***", "**", "*", "ns"))
comparacoes <- list(c("B", "C"), c("A","C")) 

pl <- ggplot(df,aes(x = Grupo, y = value, fill = Grupo)) + 
  geom_boxplot()

pl + stat_compare_means(aes(label = "p.format"),
                        method = "t.test",
                        symnum.args = symnum.args,
                        comparisons = comparacoes)

inserir a descrição da imagem aqui

To avoid overlapping lines the following example might be useful. The important part is the instructions where they are calculated lab.y1.

lab.y1 <- with(df, tapply(value, Grupo, max))
lab.y1 <- ceiling(combn(lab.y1, 2, max)) + c(0, 0.25, 1)
lab.y2 <- max(lab.y1) + 1
comparacoes2 <- list(c("A", "B"), c("B", "C"), c("A","C")) 

pl2 <- ggboxplot(df, x = "Grupo", y = "value", fill = "Grupo") +
  stat_compare_means(symnum.args = symnum.args,
                     comparisons = comparacoes2,
                     label.y = lab.y1) 

pl2

inserir a descrição da imagem aqui

  • Perfect @Rui Barradas, already checked the documentation of ggpub() and actually used method = "Anova" pq my original df consists of 4 groups. Everything is going well, except that these elements of significance ("*** " and "ns") I can’t figure out how to get to them to edit them. The lines themselves have already identified that I can move them away from each other using "label. y = c()" but when I adjust the graph for smaller visualization scales these elements of significance overlap the lines and mix. I’m still trying to figure out how to edit them!

  • 2

    @gleidsonmr See the new example. It is just a way to calculate the location of lines. But this kind of problems hardly has a general solution, it is necessary to experiment and adjust.

1

His reply @Rui Barradas, allowed me to think of a way that would change in "x" the positioning. Anyway I send the picture so you can observe what happens with the "***" and the "ns" and not exactly the lines... pq as I said earlier I can even move the lines away from each other, but the sources of these elements I could not access yet. inserir a descrição da imagem aqui

Note the figures "A,B,C and D" which are just the smallest and so are a little polluted.

The figure E due to the size, did not generate problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.