diff --git a/DESCRIPTION b/DESCRIPTION index bc53ef37..c51d25e2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: ggmice Title: Visualizations for 'mice' with 'ggplot2' -Version: 0.1.0 +Version: 0.1.0.9000 Authors@R: c( person("Hanne", "Oberman", email = "h.i.oberman@uu.nl", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-3276-2141")), diff --git a/NEWS.md b/NEWS.md index 82a4b9b0..ef10f9d5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,15 @@ +# ggmice (development version) + +## Bug fixes + +* Correct labeling of 'exclusion-restriction' variables in `plot_pred()` (#128) + +## Minor changes + +* Miscellaneous documentation and vignette updates (#128) + +--- + # ggmice 0.1.0 ## Breaking changes @@ -25,7 +37,7 @@ * Input validation for `data` argument `plot_*` functions (#85) * Input validation for `vrb` argument `plot_*` functions (#80) * Input validation for `mapping` argument `ggmice()` (#34, #90) -* Vignette updates (PRs #31, #35, #38) and other documentation (#45, #51) +* Vignette updates (#31, #35, #38) and other documentation (#45, #51) * The `plot_pattern()` function creates missing data pattern plot with more informative labels (#59, #111) --- diff --git a/R/ggmice.R b/R/ggmice.R index 70b9b6a0..3ab1862e 100644 --- a/R/ggmice.R +++ b/R/ggmice.R @@ -3,11 +3,19 @@ #' @param data An incomplete dataset (of class `data.frame`), or an object of class [`mice::mids`]. #' @param mapping A list of aesthetic mappings created with [ggplot2::aes()]. #' -#' @return An object of class [`ggplot2::ggplot`]. +#' @return An object of class [`ggplot2::ggplot`]. The [`ggmice::ggmice`] function returns output +#' 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 `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). #' #' @examples #' dat <- mice::nhanes #' ggmice(dat, ggplot2::aes(x = age, y = bmi)) + ggplot2::geom_point() +#' imp <- mice::mice(dat, print = FALSE) +#' ggmice(imp, ggplot2::aes(x = age, y = bmi)) + ggplot2::geom_point() #' @seealso See the `ggmice` vignette to use the `ggmice()` function on #' [incomplete data](https://amices.org/ggmice/articles/ggmice.html#the-ggmice-function) #' or [imputed data](https://amices.org/ggmice/articles/ggmice.html#the-ggmice-function-1). @@ -99,8 +107,8 @@ ggmice <- function(data = NULL, .imp = 0, .id = rownames(data$data), data$data - )[!miss_xy, ], - data.frame(.where = "imputed", mice::complete(data, action = "long"))[where_xy, ] + )[!miss_xy,], + data.frame(.where = "imputed", mice::complete(data, action = "long"))[where_xy,] ), .where = factor( .where, diff --git a/R/plot_pred.R b/R/plot_pred.R index 2cc425c7..f1484a87 100644 --- a/R/plot_pred.R +++ b/R/plot_pred.R @@ -50,13 +50,13 @@ plot_pred <- ind = matrix(data, nrow = p * p, byrow = TRUE) ) %>% dplyr::mutate(clr = factor( .data$ind, - levels = c(-2, 0, 1, 2, 3), + levels = c(-3, -2, 0, 1, 2), labels = c( + "inclusion-restriction variable", "cluster variable", "not used", "predictor", - "random effect", - "inclusion-restriction variable" + "random effect" ), ordered = TRUE )) @@ -78,11 +78,11 @@ plot_pred <- ) + ggplot2::scale_fill_manual( values = c( + "inclusion-restriction variable" = "orangered", "cluster variable" = "lightyellow", "not used" = "grey90", "predictor" = "palegreen3", - "random effect" = "deepskyblue", - "inclusion-restriction variable" = "orangered" + "random effect" = "deepskyblue" ) ) + ggplot2::labs( diff --git a/R/utils.R b/R/utils.R index 974e1430..2b35dba4 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,6 +1,6 @@ # Functions for internal use -# util functions +# Shorthand 'not in' for code readability `%nin%` <- Negate(`%in%`) #' Pipe operator @@ -104,3 +104,6 @@ verify_data <- function(data, } } } + +# suppress undefined global functions or variables note +utils::globalVariables(c(".id", ".imp", ".where", ".id", "where", "name", "value")) diff --git a/README.Rmd b/README.Rmd index 1db17151..ac755552 100644 --- a/README.Rmd +++ b/README.Rmd @@ -18,6 +18,7 @@ knitr::opts_chunk$set( [![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/ggmice)](https://cran.r-project.org/package=ggmice) [![Total CRAN downloads](https://cranlogs.r-pkg.org/badges/grand-total/ggmice)](https://cranlogs.r-pkg.org/badges/grand-total/ggmice) +[![r-universe status badge](https://amices.r-universe.dev/badges/ggmice)](https://amices.r-universe.dev/ggmice) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.6532702.svg)](https://doi.org/10.5281/zenodo.6532702) [![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-blue.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable) diff --git a/README.md b/README.md index 9a6b2925..acbb6602 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ [![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/ggmice)](https://cran.r-project.org/package=ggmice) [![Total CRAN downloads](https://cranlogs.r-pkg.org/badges/grand-total/ggmice)](https://cranlogs.r-pkg.org/badges/grand-total/ggmice) +[![r-universe status +badge](https://amices.r-universe.dev/badges/ggmice)](https://amices.r-universe.dev/ggmice) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.6532702.svg)](https://doi.org/10.5281/zenodo.6532702) [![Lifecycle: diff --git a/man/ggmice.Rd b/man/ggmice.Rd index 983e27ed..523e8005 100644 --- a/man/ggmice.Rd +++ b/man/ggmice.Rd @@ -12,7 +12,14 @@ ggmice(data = NULL, mapping = ggplot2::aes()) \item{mapping}{A list of aesthetic mappings created with \code{\link[ggplot2:aes]{ggplot2::aes()}}.} } \value{ -An object of class \code{\link[ggplot2:ggplot]{ggplot2::ggplot}}. +An object of class \code{\link[ggplot2:ggplot]{ggplot2::ggplot}}. The \code{\link{ggmice}} function returns output +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 \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). +} } \description{ Plot incomplete or imputed data @@ -20,6 +27,8 @@ Plot incomplete or imputed data \examples{ dat <- mice::nhanes ggmice(dat, ggplot2::aes(x = age, y = bmi)) + ggplot2::geom_point() +imp <- mice::mice(dat, print = FALSE) +ggmice(imp, ggplot2::aes(x = age, y = bmi)) + ggplot2::geom_point() } \seealso{ See the \code{ggmice} vignette to use the \code{ggmice()} function on diff --git a/vignettes/ggmice.Rmd b/vignettes/ggmice.Rmd index 10a1abc9..9331df98 100644 --- a/vignettes/ggmice.Rmd +++ b/vignettes/ggmice.Rmd @@ -90,7 +90,7 @@ The `mapping` argument in `ggmice()` cannot be empty. An `x` or `y` mapping (or ## Incomplete data -If the object supplied to the `data` argument in `ggmice()` is a `data.frame`, the visualization will contain observed data in blue and missing data in red. Since missing data points are by definition unobserved, the values themselves cannot be plotted. What we *can* plot are sets of variable pairs. Any missing values on one variable can be displayed on top of the axis of the other. This provides a visual cue that the missing data is distinct from the observed values, but still displays the observed value of the other variable. +If the object supplied to the `data` argument in `ggmice()` is a `data.frame`, the visualization will contain observed data in blue and missing data in red. Since missing data points are by definition unobserved, the values themselves cannot be plotted. What we *can* plot are sets of variable pairs. Any missing values in one variable can be displayed on the axis of the other. This provides a visual cue that the missing data is distinct from the observed values, but still displays the observed value of the other variable. For example, the variable `age` is completely observed, while there are some missing entries for the height variable `hgt`. We can create a scatter plot of these two variables with: @@ -99,7 +99,7 @@ ggmice(dat, aes(age, hgt)) + geom_point() ``` -The `age` of cases with missing `hgt` are plotted on top of the horizontal axis. This is in contrast to a regular `ggplot()` call with the same arguments, which would leave out all cases with missing `hgt`. So, with `ggmice()` we loose less information, and may even gain valuable insight into the missingness in the data. +The `age` of cases with missing `hgt` are plotted on the horizontal axis. This is in contrast to a regular `ggplot()` call with the same arguments, which would leave out all cases with missing `hgt`. So, with `ggmice()` we loose less information, and may even gain valuable insight into the missingness in the data. Another example of `ggmice()` in action on incomplete data is when one of the variables is categorical. The incomplete continuous variable `hgt` is plotted against the incomplete categorical variable `reg` with: @@ -108,7 +108,7 @@ ggmice(dat, aes(reg, hgt)) + geom_point() ``` -Again, missing values are plotted on top of the axes. Cases with observed `hgt` and missing `reg` are plotted on top of the vertical axis. Cases with observed `reg` and missing `hgt` are plotted on top of the horizontal axis. There are no cases were neither is observed, but otherwise these would be plotted on the intersection of the two axes. +Again, missing values are plotted on the axes. Cases with observed `hgt` and missing `reg` are plotted on the vertical axis. Cases with observed `reg` and missing `hgt` are plotted on the horizontal axis. There are no cases were neither is observed, but otherwise these would be plotted on the intersection of the two axes. The 'grammar of graphics' makes it easy to adjust the plots programmatically. For example, we could be interested in the differences in growth data between the city and other regions. Add facets based on a clustering variable with: