1

I am trying to add compact letter display in the boxplot I created, is there any chance to combine cldList() function with ggboxplot()?

Here is my sample data

library(FSA)
library(multcompView)
library(rcompanion)
library(ggplot2)
library(ggpubr)
library(tidyr)

df_list <- list(
  `1.3.A` = 
    tibble::tribble(
      ~Person, ~Height, ~Weight,
      "Alex",    175,     75,
      "Gerard",    110,     85,
      "Clyde",    120,     79
    ),
  `2.2.A` = 
    tibble::tribble(
      ~Person, ~Height, ~Weight,
      "Missy",    162,     55,
      "Britany",    111,     56,
      "Sussie",    192,     85
    ), 
  `1.1.B` = 
    tibble::tribble(
      ~Person, ~Height, ~Weight,
      "Luke",    177,     66,
      "Alex",    169,     69,
      "Haley",    145,     54
    )
)


lapply(df_list, function(i) ggboxplot(i, x = "Person", y = c("Height", "Weight"), combine = TRUE))
lapply(df_list, function(k) dunnTest(Weight ~ as.factor(Person), method = "bh", data = k))
lapply(df_list, function(i) cldList(P.adj ~ Comparison, threshold = 0.05))

I am trying to add significant letters per Person, in my original data, i have 30 groups to compare and adding compact letter display to the boxplot would make data interpretation much easier.

I also have multiple dataframes inside a list, was wondering if cldList() could be wrapped inside the lapply() function

I hope someone can help.

user2554330
  • 37,248
  • 4
  • 43
  • 90
  • You should add the necessary `library(...)` statements to your code. We don't know which packages you found those functions in. – user2554330 Oct 03 '21 at 10:28
  • Thanks for the reminder, added the necessary libraries. – Tyler Ruddenfort Oct 03 '21 at 11:11
  • I can run it now, but I don't know the answer. Maybe this post will help: https://stackoverflow.com/q/57737598/2554330 ? – user2554330 Oct 03 '21 at 12:03
  • Thank for the help, do you happen to understand where the `.group` in this particular line of code came from? `geom_text(aes(label = gsub(" ", "", .group))` I really cannot figure out where that come from because it is also present in this example https://schmidtpaul.github.io/DSFAIR/compactletterdisplay.html – Tyler Ruddenfort Oct 03 '21 at 12:17
  • 1
    The `.group` column was added to `mod_means` by `multcomp::cld`. It contains the letters that specify the grouping, but mixed with spaces, so `gsub()` was used to remove all the spaces. – user2554330 Oct 03 '21 at 13:04

1 Answers1

2

I am the author of the example you already mentioned in a comment. One of of the commentors already pointed out correctly where the .group column came from. However, when pointing out the general difference between your code and the one in the example I find that

  1. Data

    1a. Your data has 1 observation per factor level (=Person).

    1b. My data has multiple observations per factor level (=group).

  2. Mean comparisons

    2a. You used FSA::Dunntest to fit a model and immediately compare means per factor level.

    2b. I used lm() to fid a model, then emmeans::emmeans() to compare means per factor level.

  3. Compact Letter Display

    3a. You used rcompanion::cldList() to get the letters.

    3b. I used multcomp::cld() to get the letters.

I think point 2 and 3 are totally fine - it's simply different functions leading to the same goal. I tried your approach on my data and it worked:

dunnTest_out <- FSA::dunnTest(weight ~ as.factor(group), method = "bh", data = PlantGrowth)
rcompanion::cldList(P.adj ~ Comparison, data = dunnTest_out$res, threshold = 0.05)
#>   Group Letter MonoLetter
#> 1  ctrl     ab         ab
#> 2  trt1      a         a 
#> 3  trt2      b          b

However, your data seems off to me. You shouldn't be able to compare "means" to each other and even perform tests (whose results can be displayed via the compact letter display) if your "means" are not actually means, but single values.

I simplified your example to one of the datasets:

dat_1.1.B <- 
  tibble::tribble(
    ~Person, ~Height, ~Weight,
    "Luke",    177,     66,
    "Alex",    169,     69,
    "Haley",    145,    54
  )

dunnTest_out <- FSA::dunnTest(Weight ~ as.factor(Person), method = "bh", data = dat_1.1.B)
dunnTest_out
#> Dunn (1964) Kruskal-Wallis multiple comparison
#>   p-values adjusted with the Benjamini-Hochberg method.
#>     Comparison          Z   P.unadj     P.adj
#> 1 Alex - Haley  1.4142136 0.1572992 0.4718976
#> 2  Alex - Luke  0.7071068 0.4795001 0.4795001
#> 3 Haley - Luke -0.7071068 0.4795001 0.7192502

rcompanion::cldList(P.adj ~ Comparison, data = dunnTest_out$res, threshold = 0.05)
#> Error: No significant differences.

Note that it becomes really clear that something is not working correctly, when I change one of the Weight values to a much larger number but the p-values do not change at all.

dat_1.1.B <- 
  tibble::tribble(
    ~Person, ~Height, ~Weight,
    "Luke",    177,     66,
    "Alex",    169,     100000069,
    "Haley",    145,     54
  )

dunnTest_out <- FSA::dunnTest(Weight ~ as.factor(Person), method = "bh", data = dat_1.1.B)
dunnTest_out
#> Dunn (1964) Kruskal-Wallis multiple comparison
#>   p-values adjusted with the Benjamini-Hochberg method.
#>     Comparison          Z   P.unadj     P.adj
#> 1 Alex - Haley  1.4142136 0.1572992 0.4718976
#> 2  Alex - Luke  0.7071068 0.4795001 0.4795001
#> 3 Haley - Luke -0.7071068 0.4795001 0.7192502

rcompanion::cldList(P.adj ~ Comparison, data = dunnTest_out$res, threshold = 0.05)
#> Error: No significant differences.

So yeah, I think it's the data. Note that the error "No significant differences" is weird to me, too, because assuming the test was done correctly, no significant differences would simply mean that all values get the same letter.

tl;dr: The data is the problem. You cannot compare means via a test if your "means" are just single values per group. If you have the raw data that was used to get those single values per group, you should feed that into your model instead - as was done in my example and in the examples of ?FSA::Dunntest and ?rcompanion::cldList().

Paul Schmidt
  • 1,072
  • 10
  • 23
  • Hi @Paul, thank you very much for looking into this, actually I was able to create compact letter display list using `cldList()` function, however, it only provided letters to certain groups. BTW, thank you very much for giving a very clear explanation. I noticed you commented on my other question (here) [https://stackoverflow.com/questions/69482320/boxplot-does-not-show-significant-letters-for-some-groups], I hope you can look again – Tyler Ruddenfort Oct 08 '21 at 14:34