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)