diff --git a/R/ggmice.R b/R/ggmice.R index 3ab1862e..ddddd986 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") @@ -127,8 +137,13 @@ 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() # 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{