From 340d08789aec20fe8bf0bbe5508f384656a96208 Mon Sep 17 00:00:00 2001 From: hanneoberman Date: Mon, 22 Jan 2024 11:24:41 +0100 Subject: [PATCH 1/2] set `fill` aesthetic to `mice` colors --- R/ggmice.R | 19 ++++++++++++++++--- man/ggmice.Rd | 4 +++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/R/ggmice.R b/R/ggmice.R index 3ab1862e..1fd37d73 100644 --- a/R/ggmice.R +++ b/R/ggmice.R @@ -7,9 +7,11 @@ #' equivalent to [`ggplot2::ggplot`] output, with a few important exceptions: #' #' - The theme is set to [`ggmice::theme_mice`]. -#' - The color scale is set to the [`mice::mdc`] colors. +#' - The color and fill scales are set to the [`mice::mdc`] colors (see below). #' - The `colour` aesthetic is set to `.where`, an internally defined variable which distinguishes #' observed data from missing data or imputed data (for incomplete and imputed data, respectively). +#' - The `fill` aesthetic is set to `.where`, an internally defined variable which distinguishes +#' observed data from missing data or imputed data (for incomplete and imputed data, respectively). #' #' @examples #' dat <- mice::nhanes @@ -48,6 +50,14 @@ ggmice <- function(data = NULL, ) ) } + if (!is.null(mapping$fill)) { + cli::cli_warn( + c( + "The aes() argument 'fill' has a special use in ggmice() and will be overwritten.", + "i" = "Try using 'shape' or 'linetype' for additional mapping, or use faceting." + ) + ) + } # extract variable names from mapping object if (is.data.frame(data)) { @@ -91,7 +101,7 @@ ggmice <- function(data = NULL, ) } mice_mapping <- - utils::modifyList(mapping, ggplot2::aes(colour = .where)) + utils::modifyList(mapping, ggplot2::aes(colour = .where, fill = .where)) mice_colors <- c("observed" = "#006CC2B3", "missing" = "#B61A51B3") @@ -118,7 +128,7 @@ ggmice <- function(data = NULL, .imp = factor(.imp, levels = 0:data$m, ordered = TRUE) ) mice_mapping <- - utils::modifyList(mapping, ggplot2::aes(colour = .where)) + utils::modifyList(mapping, ggplot2::aes(colour = .where, fill = .where)) mice_colors <- c("observed" = "#006CC2B3", "imputed" = "#B61A51B3") @@ -129,6 +139,9 @@ ggmice <- function(data = NULL, ggplot2::scale_color_manual(values = mice_colors, name = "", drop = FALSE) + + ggplot2::scale_fill_manual(values = mice_colors, + name = "", + drop = FALSE) + theme_mice() # edit plot to display missing values on the axes diff --git a/man/ggmice.Rd b/man/ggmice.Rd index 523e8005..53d00523 100644 --- a/man/ggmice.Rd +++ b/man/ggmice.Rd @@ -16,9 +16,11 @@ An object of class \code{\link[ggplot2:ggplot]{ggplot2::ggplot}}. The \code{\lin equivalent to \code{\link[ggplot2:ggplot]{ggplot2::ggplot}} output, with a few important exceptions: \itemize{ \item The theme is set to \code{\link{theme_mice}}. -\item The color scale is set to the \code{\link[mice:mdc]{mice::mdc}} colors. +\item The color and fill scales are set to the \code{\link[mice:mdc]{mice::mdc}} colors (see below). \item The \code{colour} aesthetic is set to \code{.where}, an internally defined variable which distinguishes observed data from missing data or imputed data (for incomplete and imputed data, respectively). +\item The \code{fill} aesthetic is set to \code{.where}, an internally defined variable which distinguishes +observed data from missing data or imputed data (for incomplete and imputed data, respectively). } } \description{ From 9e79593969ee10b2e4849f2fd664804d491348aa Mon Sep 17 00:00:00 2001 From: hanneoberman Date: Mon, 22 Jan 2024 14:06:50 +0100 Subject: [PATCH 2/2] add `mice` color to `na.value` --- R/ggmice.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/ggmice.R b/R/ggmice.R index 1fd37d73..ddddd986 100644 --- a/R/ggmice.R +++ b/R/ggmice.R @@ -137,9 +137,11 @@ ggmice <- function(data = NULL, # create plot gg <- ggplot2::ggplot(data = mice_data, mapping = mice_mapping) + ggplot2::scale_color_manual(values = mice_colors, + na.value = "#B61A51B3", name = "", drop = FALSE) + ggplot2::scale_fill_manual(values = mice_colors, + na.value = "#B61A51B3", name = "", drop = FALSE) + theme_mice()