Skip to content

Commit

Permalink
Fix bugs in stat_multcomp()
Browse files Browse the repository at this point in the history
Fix failure to show labels bug affecting some plots with more than 5 groups and Tukey contrasts.
Fix wrongly coded letters with Tukey tests and more than 9 groups.
  • Loading branch information
aphalo committed Apr 15, 2024
1 parent f087b74 commit 970fdcf
Show file tree
Hide file tree
Showing 33 changed files with 2,983 additions and 1,785 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: ggpmisc
Type: Package
Title: Miscellaneous Extensions to 'ggplot2'
Version: 0.5.5.9001
Date: 2024-04-14
Version: 0.5.5.9002
Date: 2024-04-15
Authors@R:
c(
person("Pedro J.", "Aphalo", email = "pedro.aphalo@helsinki.fi", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-3385-972X")),
Expand All @@ -24,7 +24,7 @@ LazyLoad: TRUE
ByteCompile: TRUE
Depends:
R (>= 4.0.0),
ggpp (>= 0.5.5)
ggpp (>= 0.5.6)
Imports:
grid,
stats,
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ editor_options:
- Rename parameters of `stat_multcomp()`: `adjusted.type` to `p.adjust.method`, and `constrast.type` to `contrasts`.
- Change defaults used for `p.adjust.method` so that they depend on the argument passed to `contrasts`.
- Accept in `stat_correlation()`, `stat_poly_eq()`, `stat_ma_eq()`, and `stat_multcomp()`, `p.digits = Inf` as a request to use scientific notation for `p.value.label` (suggested by _wbvguo_, Wenbin Guo).
- Fix bug in `stat_multcomp()`: **wrongly encoded letters** in Tukey contrasts in case of grouping factors with more than nine levels.
- Fix bug in `stat_multcomp()`: off-plot letter labels with Tukey contrasts in some plots with more than five groups.

# ggpmisc 0.5.5

Expand Down
42 changes: 24 additions & 18 deletions R/stat-multcomp.R
Original file line number Diff line number Diff line change
Expand Up @@ -377,22 +377,6 @@ stat_multcomp <- function(mapping = NULL, data = NULL,
warning("\"npc\"-based geometries not supported, using\"", geom, "\" instead.")
}

if (label.type == "bars") {
if (is.null(vstep)) {
vstep <- 0.125
}
if (is.null(label.y)) {
label.y <- "top"
}
} else if (label.type %in% c("letters", "LETTERS", "numeric")) {
if (is.null(vstep)) {
vstep <- 0
}
if (is.null(label.y)) {
label.y <- "bottom"
}
}

if (is.null(output.type)) {
if (geom %in% c("richtext", "textbox")) {
output.type <- "markdown"
Expand Down Expand Up @@ -506,6 +490,24 @@ multcomp_compute_fun <-
"Resetting to 'label.type = \"letters\"'.")
label.type <- "letters"
}

# default position of labels has to be set after label.type is known
if (label.type == "bars") {
if (is.null(vstep)) {
vstep <- 0.125
}
if (is.null(label.y)) {
label.y <- "top"
}
} else if (label.type %in% c("letters", "LETTERS", "numeric")) {
if (is.null(vstep)) {
vstep <- 0
}
if (is.null(label.y)) {
label.y <- "bottom"
}
}

if (nrow(data) < 2 * num.levels) {
stop("Too few observations per factor level. ",
"Did you map to ", orientation, " a continuous variable instead of a factor?")
Expand Down Expand Up @@ -611,6 +613,9 @@ multcomp_compute_fun <-
warning(sprintf("P-value for main effect = %.3f > cutoff = %.3f, skipping tests!",
p.value, fm.cutoff.p.value))
return(data.frame())
} else if (any(is.na(fm[["coefficients"]][-1]))) {
warning("Problem with model fitting: NAs returned, skipping tests!")
return(data.frame())
}

n <- length(residuals(fm))
Expand Down Expand Up @@ -664,6 +669,7 @@ multcomp_compute_fun <-
if (is.null(p.adjust.method)) {
summary.fm.glht <-
summary(fm.glht, test = multcomp::adjusted())
# needed for labels
if (contrasts == "Tukey") {
p.adjust.method <- "HSD"
} else if (contrasts == "Dunnet"){
Expand Down Expand Up @@ -841,7 +847,7 @@ multcomp_compute_fun <-
mc.contrast = contrasts,
n = n,
letters.label = c(p.crit.label ,
Letters[order(names(Letters))]),
Letters[order(as.numeric(names(Letters)))]),
just = c("inward", rep("center", length(Letters))))
} else {
z <- tibble::tibble(x = 1:num.levels,
Expand All @@ -855,7 +861,7 @@ multcomp_compute_fun <-
mc.adjusted = p.adjust.method,
mc.contrast = contrasts,
n = n,
letters.label = Letters[order(names(Letters))],
letters.label = Letters[order(as.numeric(names(Letters)))],
just = rep("center", length(Letters)))
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
110 changes: 63 additions & 47 deletions tests/testthat/_snaps/stat-multcomp/stat-multcomp-bars-dunnet.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
200 changes: 120 additions & 80 deletions tests/testthat/_snaps/stat-multcomp/stat-multcomp-bars-tukey.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
200 changes: 120 additions & 80 deletions tests/testthat/_snaps/stat-multcomp/stat-multcomp-bonferroni-tukey.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 970fdcf

Please sign in to comment.