Skip to content

Commit

Permalink
set fill aesthetic to mice colors
Browse files Browse the repository at this point in the history
  • Loading branch information
hanneoberman committed Jan 22, 2024
1 parent 3210413 commit 340d087
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
19 changes: 16 additions & 3 deletions R/ggmice.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion man/ggmice.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 340d087

Please sign in to comment.