The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.

Old friends

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, mice, and ggplot2 packages, some incomplete data and a mids object into your workspace.

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

bwplot

Box-and-whisker plot of observed and imputed data.

# original plot
mice::bwplot(imp, hgt ~ .imp)

# ggmice equivalent
ggmice(imp, aes(x = .imp, y = hgt)) +
  geom_boxplot() +
  labs(x = "Imputation number")

# extended reproduction with ggmice
ggmice(imp, aes(x = .imp, y = hgt)) +
  stat_boxplot(geom = "errorbar", linetype = "dashed") +
  geom_boxplot(outlier.colour = "grey", outlier.shape = 1) +
  labs(x = "Imputation number") +
  theme(legend.position = "none")

densityplot

Density plot of observed and imputed data.

# original plot
mice::densityplot(imp, ~hgt)

# ggmice equivalent
ggmice(imp, aes(x = hgt, group = .imp)) +
  geom_density()

# extended reproduction with ggmice
ggmice(imp, aes(x = hgt, group = .imp, size = .where)) +
  geom_density() +
  scale_size_manual(
    values = c("observed" = 1, "imputed" = 0.5),
    guide = "none"
  ) +
  theme(legend.position = "none")

fluxplot

Influx and outflux plot of multivariate missing data patterns.

# original plot
fluxplot(dat)

# ggmice equivalent
plot_flux(dat)

md.pattern

Missing data pattern plot.

# original plot
md <- md.pattern(dat)

# ggmice equivalent
plot_pattern(dat)

# extended reproduction with ggmice
plot_pattern(dat, square = TRUE) +
  theme(
    legend.position = "none",
    axis.title = element_blank(),
    axis.title.x.top = element_blank(),
    axis.title.y.right = element_blank()
  )

plot.mids

Plot the trace lines of the MICE algorithm.

# original plot
plot(imp, hgt ~ .it | .ms)

# ggmice equivalent
plot_trace(imp, "hgt")

stripplot

Stripplot of observed and imputed data.

# original plot
mice::stripplot(imp, hgt ~ .imp)

# ggmice equivalent
ggmice(imp, aes(x = .imp, y = hgt)) +
  geom_jitter(width = 0.25) +
  labs(x = "Imputation number")

# extended reproduction with ggmice (not recommended)
ggmice(imp, aes(x = .imp, y = hgt)) +
  geom_jitter(
    shape = 1,
    width = 0.1,
    na.rm = TRUE,
    data = data.frame(
      hgt = dat$hgt,
      .imp = factor(rep(1:imp$m, each = nrow(dat))),
      .where = "observed"
    )
  ) +
  geom_jitter(shape = 1, width = 0.1) +
  labs(x = "Imputation number") +
  theme(legend.position = "none")

xyplot

Scatterplot of observed and imputed data.

# original plot
mice::xyplot(imp, hgt ~ age)

# ggmice equivalent
ggmice(imp, aes(age, hgt)) +
  geom_point()

# extended reproduction with ggmice
ggmice(imp, aes(age, hgt)) +
  geom_point(size = 2, shape = 1) +
  theme(legend.position = "none")

Extensions

Interactive plots

To make ggmice visualizations interactive, the plotly package can be used. For example, an interactive influx and outflux plot may be more legible than a static one.

# load packages
library(plotly)
# influx and outflux plot
p <- plot_flux(dat)
ggplotly(p)

Plot multiple variables

You may want to create a plot visualizing the imputations of multiple variables as one object. To visualize multiple variables at once, the variable names are saved in a vector. This vector is used together with the functional programming package purrr and visualization package patchwork to map() over the variables and subsequently wrap_plots to create a single figure.

# load packages
library(purrr)
library(patchwork)
# create vector with variable names
vrb <- names(dat)

Display box-and-whisker plots for all variables.

# original plot
mice::bwplot(imp)

# ggmice equivalent
p <- map(vrb, ~ {
  ggmice(imp, aes(x = .imp, y = .data[[.x]])) +
    geom_boxplot() +
    scale_x_discrete(drop = FALSE) +
    labs(x = "Imputation number")
})
wrap_plots(p, guides = "collect") &
  theme(legend.position = "bottom")

Display density plots for all variables.

# original plot
mice::densityplot(imp)

# ggmice equivalent
p <- map(vrb, ~ {
  ggmice(imp, aes(x = .data[[.x]], group = .imp)) +
    geom_density()
})
wrap_plots(p, guides = "collect") &
  theme(legend.position = "bottom")

Display strip plots for all variables.

# original plot
mice::stripplot(imp)

# ggmice equivalent
p <- map(vrb, ~ {
  ggmice(imp, aes(x = .imp, y = .data[[.x]])) +
    geom_jitter() +
    labs(x = "Imputation number")
})
wrap_plots(p, guides = "collect") &
  theme(legend.position = "bottom")


This is the end of the vignette. This document was generated using:

sessionInfo()
#> R version 4.3.0 (2023-04-21 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19045)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=C                       LC_CTYPE=Dutch_Netherlands.utf8   
#> [3] LC_MONETARY=Dutch_Netherlands.utf8 LC_NUMERIC=C                      
#> [5] LC_TIME=Dutch_Netherlands.utf8    
#> 
#> time zone: Europe/Amsterdam
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] patchwork_1.1.2 purrr_1.0.1     plotly_4.10.1   ggmice_0.1.0   
#> [5] ggplot2_3.4.2   mice_3.16.2    
#> 
#> loaded via a namespace (and not attached):
#>  [1] gtable_0.3.3      shape_1.4.6       xfun_0.37         bslib_0.4.2      
#>  [5] htmlwidgets_1.6.2 lattice_0.21-8    crosstalk_1.2.0   vctrs_0.6.2      
#>  [9] tools_4.3.0       generics_0.1.3    tibble_3.2.1      fansi_1.0.4      
#> [13] highr_0.10        pan_1.8           pkgconfig_2.0.3   jomo_2.7-6       
#> [17] Matrix_1.5-4.1    data.table_1.14.8 lifecycle_1.0.3   compiler_4.3.0   
#> [21] farver_2.1.1      stringr_1.5.0     munsell_0.5.0     codetools_0.2-19 
#> [25] htmltools_0.5.4   sass_0.4.6        lazyeval_0.2.2    yaml_2.3.7       
#> [29] glmnet_4.1-7      pillar_1.9.0      nloptr_2.0.3      jquerylib_0.1.4  
#> [33] tidyr_1.3.0       ellipsis_0.3.2    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       labeling_0.4.2    splines_4.3.0    
#> [49] fastmap_1.1.1     grid_4.3.0        colorspace_2.1-0  cli_3.6.1        
#> [53] magrittr_2.0.3    survival_3.5-5    utf8_1.2.3        broom_1.0.5      
#> [57] withr_2.5.0       scales_1.2.1      backports_1.4.1   rmarkdown_2.21   
#> [61] httr_1.4.6        nnet_7.3-18       lme4_1.1-34       evaluate_0.21    
#> [65] knitr_1.42        viridisLite_0.4.2 rlang_1.1.1       Rcpp_1.0.10      
#> [69] glue_1.6.2        rstudioapi_0.14   minqa_1.2.5       jsonlite_1.8.7   
#> [73] R6_2.5.1

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.