Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set fill aesthetic to mice colors #142

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 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 @@ -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
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.