Skip to contents

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.

# load packages
library(ggmice)
# load incomplete dataset 
dat <- mice::boys
# generate imputations
imp <- mice::mice(dat, method = "pmm", printFlag = FALSE)

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")

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.2.2 (2022-10-31)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 22.04.1 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
#> 
#> 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   
#> 
#> 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] Rcpp_1.0.10       highr_0.10        bslib_0.4.2       compiler_4.2.2   
#>  [5] pillar_1.8.1      jquerylib_0.1.4   tools_4.2.2       digest_0.6.31    
#>  [9] gtable_0.3.1      lattice_0.20-45   jsonlite_1.8.4    evaluate_0.20    
#> [13] memoise_2.0.1     lifecycle_1.0.3   tibble_3.1.8      pkgconfig_2.0.3  
#> [17] rlang_1.0.6       cli_3.6.0         yaml_2.3.7        pkgdown_2.0.7    
#> [21] xfun_0.36         fastmap_1.1.0     withr_2.5.0       stringr_1.5.0    
#> [25] dplyr_1.0.10      knitr_1.42        desc_1.4.2        generics_0.1.3   
#> [29] fs_1.6.0          vctrs_0.5.2       sass_0.4.5        systemfonts_1.0.4
#> [33] grid_4.2.2        tidyselect_1.2.0  rprojroot_2.0.3   glue_1.6.2       
#> [37] mice_3.15.0       R6_2.5.1          textshaping_0.3.6 fansi_1.0.4      
#> [41] rmarkdown_2.20    farver_2.1.1      ggplot2_3.4.0     tidyr_1.3.0      
#> [45] purrr_1.0.1       magrittr_2.0.3    MASS_7.3-58.1     scales_1.2.1     
#> [49] backports_1.4.1   htmltools_0.5.4   colorspace_2.1-0  labeling_0.4.2   
#> [53] ragg_1.2.5        utf8_1.2.2        stringi_1.7.12    munsell_0.5.0    
#> [57] cachem_1.0.6      broom_1.0.3