From 524b1d3eb409649bfd4454378752a625bd825cf2 Mon Sep 17 00:00:00 2001 From: pepijnvink Date: Thu, 14 Dec 2023 12:45:51 +0100 Subject: [PATCH 1/2] add 'grid' argument for plot_pattern, plot_pred, and plot_corr --- R/plot_corr.R | 10 ++++++++-- R/plot_pattern.R | 10 ++++++++-- R/plot_pred.R | 11 ++++++++--- man/plot_corr.Rd | 5 ++++- man/plot_pattern.Rd | 5 ++++- man/plot_pred.Rd | 5 ++++- tests/testthat/test-plot_corr.R | 3 ++- tests/testthat/test-plot_pattern.R | 2 +- tests/testthat/test-plot_pred.R | 3 ++- 9 files changed, 41 insertions(+), 13 deletions(-) diff --git a/R/plot_corr.R b/R/plot_corr.R index 29f7d957..8fe30bf8 100644 --- a/R/plot_corr.R +++ b/R/plot_corr.R @@ -7,6 +7,7 @@ #' @param diagonal Logical indicating whether the correlation of each variable with itself should be displayed. #' @param rotate Logical indicating whether the variable name labels should be rotated 90 degrees. #' @param caption Logical indicating whether the figure caption should be displayed. +#' @param grid Logical indicating whether borders should be present between tiles. #' #' @return An object of class [ggplot2::ggplot]. #' @@ -20,7 +21,8 @@ plot_corr <- square = TRUE, diagonal = FALSE, rotate = FALSE, - caption = TRUE) { + caption = TRUE, + grid = TRUE) { if (is.matrix(data) && ncol(data) > 1) { data <- as.data.frame(data) } @@ -73,7 +75,6 @@ plot_corr <- label = .data$corr, fill = .data$corr )) + - ggplot2::geom_tile(color = "black", alpha = 0.6) + ggplot2::scale_x_discrete(limits = vrb, position = "top") + ggplot2::scale_y_discrete(limits = rev(vrb)) + ggplot2::scale_fill_gradient2( @@ -84,6 +85,11 @@ plot_corr <- limits = c(-1, 1) ) + theme_minimice() + if (grid) { + gg <- gg + ggplot2::geom_tile(color = "black", alpha = 0.6) + } else { + gg <- gg + ggplot2::geom_tile(alpha = 0.6) + } if (caption) { gg <- gg + ggplot2::labs( diff --git a/R/plot_pattern.R b/R/plot_pattern.R index 06342e6c..c6534e47 100644 --- a/R/plot_pattern.R +++ b/R/plot_pattern.R @@ -7,6 +7,7 @@ #' @param cluster Optional character string specifying which variable should be used for clustering (e.g., for multilevel data). #' @param npat Optional numeric input specifying the number of missing data patterns to be visualized, defaults to all patterns. #' @param caption Logical indicating whether the figure caption should be displayed. +#' @param grid Logical indicating whether borders should be present between tiles. #' #' @return An object of class [ggplot2::ggplot]. #' @@ -20,7 +21,8 @@ plot_pattern <- rotate = FALSE, cluster = NULL, npat = NULL, - caption = TRUE) { + caption = TRUE, + grid = TRUE) { # input processing if (is.matrix(data) && ncol(data) > 1) { data <- as.data.frame(data) @@ -150,7 +152,6 @@ plot_pattern <- alpha = 0.1 + .data$.opacity / 2 ) ) + - ggplot2::geom_tile(color = "black") + ggplot2::scale_fill_manual(values = c( "observed" = "#006CC2B3", "missing" = "#B61A51B3" @@ -177,6 +178,11 @@ plot_pattern <- theme_minimice() # additional arguments + if (grid) { + gg <- gg + ggplot2::geom_tile(color = "black") + } else { + gg <- gg + ggplot2::geom_tile() + } if (square) { gg <- gg + ggplot2::coord_fixed(expand = FALSE) } else { diff --git a/R/plot_pred.R b/R/plot_pred.R index 2cc425c7..1793d602 100644 --- a/R/plot_pred.R +++ b/R/plot_pred.R @@ -6,6 +6,7 @@ #' @param label Logical indicating whether predictor matrix values should be displayed. #' @param square Logical indicating whether the plot tiles should be squares. #' @param rotate Logical indicating whether the variable name labels should be rotated 90 degrees. +#' @param grid Logical indicating whether borders should be present between tiles. #' #' @return An object of class `ggplot2::ggplot`. #' @@ -19,7 +20,8 @@ plot_pred <- method = NULL, label = TRUE, square = TRUE, - rotate = FALSE) { + rotate = FALSE, + grid = TRUE) { verify_data(data, pred = TRUE) p <- nrow(data) if (!is.null(method) && is.character(method)) { @@ -69,7 +71,6 @@ plot_pred <- label = .data$ind, fill = .data$clr )) + - ggplot2::geom_tile(color = "black", alpha = 0.6) + ggplot2::scale_x_discrete(limits = vrbs, position = "top") + ggplot2::scale_y_reverse( breaks = 1:p, @@ -92,7 +93,11 @@ plot_pred <- color = "" ) + theme_minimice() - + if (grid) { + gg <- gg + ggplot2::geom_tile(color = "black", alpha = 0.6) + } else { + gg <- gg + ggplot2::geom_tile(alpha = 0.6) + } if (all(method == "")) { gg <- gg + ggplot2::theme(axis.ticks.y.right = ggplot2::element_blank()) diff --git a/man/plot_corr.Rd b/man/plot_corr.Rd index fa57995d..76fcda43 100644 --- a/man/plot_corr.Rd +++ b/man/plot_corr.Rd @@ -11,7 +11,8 @@ plot_corr( square = TRUE, diagonal = FALSE, rotate = FALSE, - caption = TRUE + caption = TRUE, + grid = TRUE ) } \arguments{ @@ -28,6 +29,8 @@ plot_corr( \item{rotate}{Logical indicating whether the variable name labels should be rotated 90 degrees.} \item{caption}{Logical indicating whether the figure caption should be displayed.} + +\item{grid}{Logical indicating whether borders should be present between tiles.} } \value{ An object of class \link[ggplot2:ggplot]{ggplot2::ggplot}. diff --git a/man/plot_pattern.Rd b/man/plot_pattern.Rd index 3d27a04a..79992f6b 100644 --- a/man/plot_pattern.Rd +++ b/man/plot_pattern.Rd @@ -11,7 +11,8 @@ plot_pattern( rotate = FALSE, cluster = NULL, npat = NULL, - caption = TRUE + caption = TRUE, + grid = TRUE ) } \arguments{ @@ -28,6 +29,8 @@ plot_pattern( \item{npat}{Optional numeric input specifying the number of missing data patterns to be visualized, defaults to all patterns.} \item{caption}{Logical indicating whether the figure caption should be displayed.} + +\item{grid}{Logical indicating whether borders should be present between tiles.} } \value{ An object of class \link[ggplot2:ggplot]{ggplot2::ggplot}. diff --git a/man/plot_pred.Rd b/man/plot_pred.Rd index c71970a6..6b91ca63 100644 --- a/man/plot_pred.Rd +++ b/man/plot_pred.Rd @@ -10,7 +10,8 @@ plot_pred( method = NULL, label = TRUE, square = TRUE, - rotate = FALSE + rotate = FALSE, + grid = TRUE ) } \arguments{ @@ -25,6 +26,8 @@ plot_pred( \item{square}{Logical indicating whether the plot tiles should be squares.} \item{rotate}{Logical indicating whether the variable name labels should be rotated 90 degrees.} + +\item{grid}{Logical indicating whether borders should be present between tiles.} } \value{ An object of class \code{ggplot2::ggplot}. diff --git a/tests/testthat/test-plot_corr.R b/tests/testthat/test-plot_corr.R index 6e5f7ca3..5801262f 100644 --- a/tests/testthat/test-plot_corr.R +++ b/tests/testthat/test-plot_corr.R @@ -9,7 +9,8 @@ test_that("plot_corr creates ggplot object", { label = TRUE, square = FALSE, diagonal = TRUE, - rotate = TRUE + rotate = TRUE, + grid = FALSE ), "ggplot") }) diff --git a/tests/testthat/test-plot_pattern.R b/tests/testthat/test-plot_pattern.R index 3d98fd92..52f2e6e6 100644 --- a/tests/testthat/test-plot_pattern.R +++ b/tests/testthat/test-plot_pattern.R @@ -4,7 +4,7 @@ dat <- mice::nhanes # tests test_that("plot_pattern produces plot", { expect_s3_class(plot_pattern(dat), "ggplot") - expect_s3_class(plot_pattern(dat, square = FALSE, rotate = TRUE, cluster = "age", npat = 2), "ggplot") + expect_s3_class(plot_pattern(dat, square = FALSE, rotate = TRUE, cluster = "age", grid = FALSE, npat = 2), "ggplot") expect_s3_class(plot_pattern(cbind(dat, "testvar" = NA), caption = FALSE), "ggplot") }) diff --git a/tests/testthat/test-plot_pred.R b/tests/testthat/test-plot_pred.R index 57d72649..3ce057a5 100644 --- a/tests/testthat/test-plot_pred.R +++ b/tests/testthat/test-plot_pred.R @@ -10,7 +10,8 @@ test_that("plot_pred creates ggplot object", { method = c("pmm"), label = FALSE, square = FALSE, - rotate = TRUE + rotate = TRUE, + grid = FALSE ), "ggplot") expect_s3_class(plot_pred(pred, vrb = c("age", "bmi")), "ggplot") From 06ab13265881e037c816e81b7f04e5b3ccbcc24d Mon Sep 17 00:00:00 2001 From: pepijnvink Date: Tue, 20 Feb 2024 17:42:03 +0100 Subject: [PATCH 2/2] `ggplot2::geom_raster()` if grid = TRUE Instead of `ggplot2::geom_tile`. Avoids awkward coloring with many tiles. --- R/plot_pattern.R | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/R/plot_pattern.R b/R/plot_pattern.R index c6534e47..d262d15c 100644 --- a/R/plot_pattern.R +++ b/R/plot_pattern.R @@ -181,7 +181,7 @@ plot_pattern <- if (grid) { gg <- gg + ggplot2::geom_tile(color = "black") } else { - gg <- gg + ggplot2::geom_tile() + gg <- gg + ggplot2::geom_raster() } if (square) { gg <- gg + ggplot2::coord_fixed(expand = FALSE) @@ -215,7 +215,6 @@ plot_pattern <- pat[rws, cls])) } } - return(gg) }