aphalo / ggpmisc

R package ggpmisc is an extension to ggplot2 and the Grammar of Graphics
https://docs.r4photobiology.info/ggpmisc
94 stars 6 forks source link

`stat_multcomp()` Enhance multiple comparisions and contrasts #44

Closed aphalo closed 4 months ago

aphalo commented 1 year ago

Add "staircase" pairwise contrasts. Test with additional model fit functions.

markbneal commented 9 months ago

For multiple comparisons, I note you have allowed the use of a number of corrections, e.g Bonferroni. It seems like it would be useful to be able to note the methodology in a caption. @JosiahCle, you might have some ideas on this based on your experience automating captions?

image

aphalo commented 9 months ago

This is a good point, but would be easier to use a text label in the plotting area, at least together with letters, and a subscript to P when the P-value is shown. Stats in principle pass data to a geom that adds a plot layer. So, even if possible, it would not follow the grammar of graphics.

aphalo commented 9 months ago

This is a good point, but would be easier to use a text label in the plotting area, at least together with letters, and a subscript to P when the P-value is shown. Stats in principle pass data to a geom that adds a plot layer. So, even if possible, it would not follow the grammar of graphics.

I just implemented labels as shown below. Do you think this would work instead of the suggested caption?

library(ggpmisc)
#> Loading required package: ggpp
#> Loading required package: ggplot2
#> Registered S3 methods overwritten by 'ggpp':
#>   method                  from   
#>   heightDetails.titleGrob ggplot2
#>   widthDetails.titleGrob  ggplot2
#> 
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#> 
#>     annotate
#> Registered S3 method overwritten by 'ggpmisc':
#>   method                  from   
#>   as.character.polynomial polynom

p1 <- ggplot(mpg, aes(factor(cyl), hwy)) +
   geom_boxplot(width = 0.33)

p1 +
  stat_multcomp(adjusted.type = "bonferroni")


p1 +
  stat_multcomp(adjusted.type = "bonferroni", adj.method.tag = 10)


p1 +
  stat_multcomp(adjusted.type = "bonferroni", adj.method.tag = 0)


p1 +
  stat_multcomp(adjusted.type = "bonferroni", adj.method.tag = -1)


p1 +
  stat_multcomp(label.type = "LETTERS", adjusted.type = "holm")


p1 +
  stat_multcomp(label.type = "LETTERS", adjusted.type = "bonferroni", mc.critical.p.value = 0.01)

Created on 2023-11-23 with reprex v2.0.2

markbneal commented 9 months ago

Those options work, and are better than staying silent on whether an adjustment has been implemented to determine the p value. In the multiple pair wise comparisons, it feels like it starts to take up realestate with the full bonferroni when saying once would be enough, but I take your point that it doesn't fit neatly with grammar of graphics. Your abbreviation goes someway to making it tidy.

If I was coding for transparency, I could imagine before the plot I set a variable: Adjustment_method <- "bonferonni"

Then in the plot call, I use that variable as a parameter to stat_multcomp, and as a parameter for my caption.

aphalo commented 9 months ago

Thanks! Yes, I agree with you that the option of setting the caption manually is a good one, and should be supported. The abbreviations are generated with R function abbreviate(). I think the default should be to add the label, but allow the user to skip it. Thus a couple of tweaks to the code.

Now a negative value as argument, such as adj.method.tag = -3 uses an abbreviation of word "adjusted": abbreviate("adjusted", -adj.method.tag). Now adj.method.tag = 0 or adj.method.tag = FALSE when using letters to show significant differences removes the label so that, for example, the information can be shown in the caption instead of on the plot.

library(ggpmisc)
#> Loading required package: ggpp
#> Loading required package: ggplot2
#> Registered S3 methods overwritten by 'ggpp':
#>   method                  from   
#>   heightDetails.titleGrob ggplot2
#>   widthDetails.titleGrob  ggplot2
#> 
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#> 
#>     annotate
#> Registered S3 method overwritten by 'ggpmisc':
#>   method                  from   
#>   as.character.polynomial polynom

p1 <- ggplot(mpg, aes(factor(cyl), hwy)) +
  geom_boxplot(width = 0.33)

p1 +
  stat_multcomp(adjusted.type = "bonferroni", adj.method.tag = -3)


p1 +
  stat_multcomp(adjusted.type = "bonferroni", adj.method.tag = 0)


p1 +
  stat_multcomp(label.type = "LETTERS", adjusted.type = "holm")


p1 +
  stat_multcomp(label.type = "LETTERS", adjusted.type = "holm", adj.method.tag = -3)


p1 +
  stat_multcomp(label.type = "LETTERS", adjusted.type = "holm", adj.method.tag = 0)

Created on 2023-11-23 with reprex v2.0.2

aphalo commented 9 months ago

This is only implemented for expressions. After additional testing I will add the equivalent code for LaTeX and Markdown labels.

aphalo commented 9 months ago

Add "staircase" pairwise contrasts. These and other arbitrary pairwise contrasts are now implemented by accepting a numeric matrix describing arbitrary sets of pairwise contrasts.

aphalo commented 4 months ago

Adding a 'ggplot2' caption from within a statistic is not consistent with the Grammar of Graphics. Seems also difficult to implement without using fragile coding tricks. Geoms are expected to only add plot layers.