---
title: "Old friends"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{old_friends}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---
# 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.

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7.2,
  fig.height = 4,
  warning = FALSE,
  message = FALSE
)
options(rmarkdown.html_vignette.check_title = FALSE)
```

First load the `ggmice`, `mice`, and `ggplot2` packages, some incomplete data and a `mids` object into your workspace. 

```{r setup, warning = FALSE, message = FALSE}
# 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.

```{r bwplot}
# 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.

```{r densityplot}
# 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, linewidth = .where)) +
  geom_density() +
  scale_linewidth_manual(
    values = c("observed" = 1, "imputed" = 0.5),
    guide = "none"
  ) +
  theme(legend.position = "none")
```

# `fluxplot`

Influx and outflux plot of multivariate missing data patterns.

```{r flux}
# original plot
fluxplot(dat)
# ggmice equivalent
plot_flux(dat)
```


# `md.pattern`

Missing data pattern plot.

```{r md.pattern}
# 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.

```{r plot.mids}
# original plot
plot(imp, hgt ~ .it | .ms)
# ggmice equivalent
plot_trace(imp, "hgt")
```

# `stripplot`

Stripplot of observed and imputed data.

```{r stripplot}
# 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.

```{r}
# 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.

```{r plotly}
# 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.

```{r mapping}
# load packages
library(purrr)
library(patchwork)
# create vector with variable names
vrb <- names(dat)
```

Display box-and-whisker plots for all variables.

```{r bwplots}
# 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.

```{r densityplots, message=FALSE, warning=FALSE}
# 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.

```{r stripplots}
# 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:

```{r session, class.source = 'fold-hide'}
sessionInfo()
```
