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.
Jump to
Jump to file
Failed to load files.
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 @@
)
)
}
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 @@
)
}
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 @@ -107,8 +117,8 @@
.imp = 0,
.id = rownames(data$data),
data$data
)[!miss_xy,],

Check warning on line 120 in R/ggmice.R

View workflow job for this annotation

GitHub Actions / lint

file=R/ggmice.R,line=120,col=20,[commas_linter] Commas should always have a space after.
data.frame(.where = "imputed", mice::complete(data, action = "long"))[where_xy,]

Check warning on line 121 in R/ggmice.R

View workflow job for this annotation

GitHub Actions / lint

file=R/ggmice.R,line=121,col=88,[commas_linter] Commas should always have a space after.
),
.where = factor(
.where,
Expand All @@ -118,7 +128,7 @@
.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,13 +137,18 @@
# 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
if (is.data.frame(data) &&
!is.null(mapping$x) && !is.null(mapping$y)) {

Check warning on line 151 in R/ggmice.R

View workflow job for this annotation

GitHub Actions / lint

file=R/ggmice.R,line=151,col=6,[indentation_linter] Indentation should be 8 spaces but is 6 spaces.
gg <- gg +
ggplot2::coord_cartesian(clip = "off")
if (!is.null(mapping$x)) {
Expand Down Expand Up @@ -187,14 +202,14 @@
mapping_out <- mapping_text
}
if ((mice::is.mids(data) &&
mapping_text %in% c(".id", ".imp", ".where"))) {

Check warning on line 205 in R/ggmice.R

View workflow job for this annotation

GitHub Actions / lint

file=R/ggmice.R,line=205,col=7,[indentation_linter] Indentation should be 9 spaces but is 7 spaces.
mapping_out <- NULL
}
if (!is.null(mapping_in) &&
mapping_text %nin% c(vrbs, ".id", ".imp", ".where")) {

Check warning on line 209 in R/ggmice.R

View workflow job for this annotation

GitHub Actions / lint

file=R/ggmice.R,line=209,col=6,[indentation_linter] Indentation should be 8 spaces but is 6 spaces.
mapping_out <- vrbs[stringr::str_detect(mapping_text, vrbs)]
if (identical(mapping_out, character(0)) ||
inherits(try(dplyr::mutate(mapping_data,

Check warning on line 212 in R/ggmice.R

View workflow job for this annotation

GitHub Actions / lint

file=R/ggmice.R,line=212,col=8,[indentation_linter] Indentation should be 10 spaces but is 8 spaces.
!!rlang::parse_quo(mapping_text, env = rlang::current_env())),
silent = TRUE)
,
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.