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.0 (2022-04-22)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 20.04.4 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.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   
#> 
#> 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] tidyselect_1.1.2  xfun_0.31         bslib_0.3.1       purrr_0.3.4      
#>  [5] lattice_0.20-45   colorspace_2.0-3  vctrs_0.4.1       generics_0.1.2   
#>  [9] htmltools_0.5.2   yaml_2.3.5        utf8_1.2.2        rlang_1.0.2      
#> [13] pkgdown_2.0.3     jquerylib_0.1.4   pillar_1.7.0      glue_1.6.2       
#> [17] withr_2.5.0       lifecycle_1.0.1   stringr_1.4.0     munsell_0.5.0    
#> [21] gtable_0.3.0      ragg_1.2.2        memoise_2.0.1     evaluate_0.15    
#> [25] labeling_0.4.2    knitr_1.39        fastmap_1.1.0     fansi_1.0.3      
#> [29] highr_0.9         broom_0.8.0       Rcpp_1.0.8.3      backports_1.4.1  
#> [33] scales_1.2.0      cachem_1.0.6      desc_1.4.1        jsonlite_1.8.0   
#> [37] farver_2.1.0      systemfonts_1.0.4 fs_1.5.2          textshaping_0.3.6
#> [41] ggplot2_3.3.6     digest_0.6.29     stringi_1.7.6     dplyr_1.0.9      
#> [45] rprojroot_2.0.3   grid_4.2.0        cli_3.3.0         tools_4.2.0      
#> [49] magrittr_2.0.3    sass_0.4.1        tibble_3.1.7      mice_3.14.0      
#> [53] crayon_1.5.1      tidyr_1.2.0       pkgconfig_2.0.3   MASS_7.3-57      
#> [57] ellipsis_0.3.2    rmarkdown_2.14    R6_2.5.1          compiler_4.2.0