Create the ggmice
equivalent of mice
plots
How to re-create the output of the plotting functions from
mice
with ggmice
. In alphabetical order of the
mice
functions.
First load the ggmice
package, some incomplete data and
a mice::mids
object into your workspace.
bwplot
Box-and-whisker plot of observed and imputed data.
# original plot
mice::bwplot(imp, bmi ~ .imp)
# ggmice equivalent
ggmice(imp, ggplot2::aes(x = .imp, y = bmi)) +
ggplot2::geom_boxplot() +
ggplot2::labs(x = "Imputation number")
# extended reproduction with ggmice
ggmice(imp, ggplot2::aes(x = .imp, y = bmi)) +
ggplot2::stat_boxplot(geom = 'errorbar', linetype = "dashed") +
ggplot2::geom_boxplot(outlier.colour = "grey", outlier.shape = 1) +
ggplot2::labs(x = "Imputation number") +
ggplot2::theme(legend.position = "none")
densityplot
Density plot of observed and imputed data.
# original plot
mice::densityplot(imp, ~bmi)
# ggmice equivalent
ggmice(imp, ggplot2::aes(x = bmi, group = .imp)) +
ggplot2::geom_density()
# extended reproduction with ggmice
ggmice(imp, ggplot2::aes(x = bmi, group = .imp, size = .where)) +
ggplot2::geom_density() +
ggplot2::scale_size_manual(values = c("observed" = 1, "imputed" = 0.5),
guide = "none") +
ggplot2::theme(legend.position = "none")
fluxplot
Influx and outflux plot of multivariate missing data patterns.
# original plot
mice::fluxplot(dat)
# ggmice equivalent
plot_flux(dat)
md.pattern
Missing data pattern plot.
# original plot
md <- mice::md.pattern(dat)
# ggmice equivalent
plot_pattern(dat)
# extended reproduction with ggmice
plot_pattern(dat, square = TRUE) +
ggplot2::theme(legend.position = "none",
axis.title = ggplot2::element_blank(),
axis.title.x.top = ggplot2::element_blank(),
axis.title.y.right = ggplot2::element_blank())
plot.mids
Plot the trace lines of the MICE algorithm.
# original plot
plot(imp, bmi ~ .it | .ms)
# ggmice equivalent
plot_trace(imp, "bmi")
stripplot
Stripplot of observed and imputed data.
# original plot
mice::stripplot(imp, bmi ~ .imp)
# ggmice equivalent
ggmice(imp, ggplot2::aes(x = .imp, y = bmi)) +
ggplot2::geom_jitter(width = 0.25) +
ggplot2::labs(x = "Imputation number")
# extended reproduction with ggmice (not recommended)
ggmice(imp, ggplot2::aes(x = .imp, y = bmi)) +
ggplot2::geom_jitter(
shape = 1,
width = 0.1,
na.rm = TRUE,
data = data.frame(
bmi = dat$bmi,
.imp = factor(rep(1:imp$m, each = nrow(dat))),
.where = "observed"
)
) +
ggplot2::geom_jitter(shape = 1, width = 0.1) +
ggplot2::labs(x = "Imputation number") +
ggplot2::theme(legend.position = "none")
Faceted stripplot for multiple variables.
# original plot
mice::stripplot(imp, wgt + hgt + bmi ~ .imp)
# ggmice equivalent
purrr::map(c("wgt", "hgt", "bmi"), ~{
# ggmice equivalent
ggmice(imp, ggplot2::aes(x = .imp, y = .data[[.x]])) +
ggplot2::geom_jitter(height = 0, width = 0.25) +
ggplot2::labs(x = "Imputation number")
}) %>%
patchwork::wrap_plots()
xyplot
Scatterplot of observed and imputed data.
# original plot
mice::xyplot(imp, bmi ~ age)
# ggmice equivalent
ggmice(imp, ggplot2::aes(age, bmi)) +
ggplot2::geom_point()
# extended reproduction with ggmice
ggmice(imp, ggplot2::aes(age, bmi)) +
ggplot2::geom_point(size = 2, shape = 1) +
ggplot2::theme(legend.position = "none")
This is the end of the vignette. This document was generated using:
sessionInfo()
#> R version 4.3.0 (2023-04-21)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 22.04.2 LTS
#>
#> Matrix products: default
#> BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
#> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so; LAPACK version 3.10.0
#>
#> locale:
#> [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
#> [4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
#> [7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
#> [10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
#>
#> time zone: UTC
#> tzcode source: system (glibc)
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] ggmice_0.0.1.9000
#>
#> loaded via a namespace (and not attached):
#> [1] gtable_0.3.3 shape_1.4.6 xfun_0.39 bslib_0.4.2
#> [5] ggplot2_3.4.2 lattice_0.21-8 vctrs_0.6.2 tools_4.3.0
#> [9] generics_0.1.3 tibble_3.2.1 fansi_1.0.4 highr_0.10
#> [13] pan_1.6 pkgconfig_2.0.3 jomo_2.7-6 Matrix_1.5-4
#> [17] desc_1.4.2 lifecycle_1.0.3 farver_2.1.1 compiler_4.3.0
#> [21] stringr_1.5.0 textshaping_0.3.6 munsell_0.5.0 codetools_0.2-19
#> [25] htmltools_0.5.5 sass_0.4.6 yaml_2.3.7 glmnet_4.1-7
#> [29] mice_3.16.0 pillar_1.9.0 pkgdown_2.0.7 nloptr_2.0.3
#> [33] jquerylib_0.1.4 tidyr_1.3.0 MASS_7.3-58.4 cachem_1.0.8
#> [37] iterators_1.0.14 rpart_4.1.19 boot_1.3-28.1 foreach_1.5.2
#> [41] mitml_0.4-5 nlme_3.1-162 tidyselect_1.2.0 digest_0.6.31
#> [45] stringi_1.7.12 dplyr_1.1.2 purrr_1.0.1 labeling_0.4.2
#> [49] splines_4.3.0 rprojroot_2.0.3 fastmap_1.1.1 grid_4.3.0
#> [53] colorspace_2.1-0 cli_3.6.1 magrittr_2.0.3 patchwork_1.1.2
#> [57] survival_3.5-5 utf8_1.2.3 broom_1.0.4 withr_2.5.0
#> [61] scales_1.2.1 backports_1.4.1 rmarkdown_2.22 nnet_7.3-18
#> [65] lme4_1.1-33 ragg_1.2.5 memoise_2.0.1 evaluate_0.21
#> [69] knitr_1.43 rlang_1.1.1 Rcpp_1.0.10 glue_1.6.2
#> [73] minqa_1.2.5 jsonlite_1.8.5 R6_2.5.1 systemfonts_1.0.4
#> [77] fs_1.6.2