---
title: "Modifying the default plot"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Modifying the default plot}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---




```r
library(inlamemi)
library(ggplot2)
```

Once we have fitted an `inlamemi` model, we can use the `inlamemi` plot method to see a visual summary of the estimated coefficients and their 95% credible intervals.

As an example, we use a data set with missingness in one variable. For the model, we will then have three levels: the model of interest, the imputation model, and the missingness model. 


```r
mis_mod <- fit_inlamemi(formula_moi = y ~ x + z1 + z2,
                          formula_imp = x ~ z1,
                          formula_mis = m ~ z2 + x,
                          family_moi = "gaussian",
                          data = mar_data,
                          error_type = "missing",
                          prior.beta.error = c(0, 1/1000),
                          prior.gamma.error = c(0, 1/1000),
                          prior.prec.moi = c(10, 9),
                          prior.prec.imp = c(10, 9),
                          initial.prec.moi = 1,
                          initial.prec.imp = 1)
```

This is the default plot:

```r
plot(mis_mod)
```

![plot of chunk unnamed-chunk-3](figure/unnamed-chunk-3-1.png)
The `plot()` function itself has some arguments that can be used to exclude sub-models, or to create a circle highlighting the coefficients of the variable with error or missingness. There is also an option to make the coefficient names into greek letters:

```r
plot(mis_mod, greek_coefficients = TRUE)
```

![plot of chunk unnamed-chunk-4](figure/unnamed-chunk-4-1.png)


But there are also some further modifications that can be made to the plot object, which is an object of class `ggplot`:


```r
mis_plot <- plot(mis_mod)
class(mis_plot)
#> [1] "gg"     "ggplot"
```

This means that we can use standard `ggplot2` functions to build on this plot.

If you are familiar with `ggplot2`, you know that it takes a data frame, and then it lets us map the data in the columns very elegantly in whichever way we specify. That means that the plot created above has also been created based on a data frame, and in order to modify the object further, we need to know the names of the columns, in order to refer to them correctly. We can access the data frame like this:


```r
mis_plot$data
#>            coefficient_type        mean         sd quant_0.025    0.5quant quant_0.975        mode variable_raw        model_type
#> beta.0             moi_coef  1.03972265 0.04998303   0.9411588  1.03983052   1.1377598  1.03985423       beta.0 Model of interest
#> beta.z1            moi_coef  2.00443459 0.03646231   1.9329016  2.00444097   2.0759309  2.00444086      beta.z1 Model of interest
#> beta.z2            moi_coef  1.98532334 0.03637896   1.9139728  1.98532291   2.0566763  1.98532290      beta.z2 Model of interest
#> beta.x           error_coef  1.95348404 0.03469568   1.8857563  1.95328709   2.0223651  1.95246286       beta.x Model of interest
#> gamma.x          error_coef -0.03594416 0.08911662  -0.2114785 -0.03591242   0.1394060 -0.03578129      gamma.x Missingness model
#> alpha.x.0          imp_coef  1.01052405 0.03222237   0.9473174  1.01052704   1.0737137  1.01052707    alpha.x.0  Imputation model
#> alpha.x.z1         imp_coef  0.29153865 0.03253326   0.2277285  0.29153940   0.3553445  0.29153941   alpha.x.z1  Imputation model
#> gamma.x.0          mis_coef -1.50922068 0.12208910  -1.7534713 -1.50784917  -1.2735610 -1.50785666    gamma.x.0 Missingness model
#> gamma.x.z2         mis_coef -0.42466592 0.08800230  -0.5972265 -0.42466734  -0.2520973 -0.42466737   gamma.x.z2 Missingness model
#>            error_indicator  var1 var2 variable_greek   variable
#> beta.0                   0  beta    0        beta[0]     beta.0
#> beta.z1                  0  beta   z1       beta[z1]    beta.z1
#> beta.z2                  0  beta   z2       beta[z2]    beta.z2
#> beta.x                   1  beta    x        beta[x]     beta.x
#> gamma.x                  1 gamma    x       gamma[x]    gamma.x
#> alpha.x.0                0 alpha    0       alpha[0]  alpha.x.0
#> alpha.x.z1               0 alpha   z1      alpha[z1] alpha.x.z1
#> gamma.x.0                0 gamma    0       gamma[0]  gamma.x.0
#> gamma.x.z2               0 gamma   z2      gamma[z2] gamma.x.z2
```

Now, let's say we would like to have three separate plots, one for each sub-model. We could do this using `facet_wrap()`:


```r
mis_plot +
  facet_wrap(~model_type, scales = "free")
```

![plot of chunk unnamed-chunk-7](figure/unnamed-chunk-7-1.png)

We can make the same plot with the greek names:


```r
plot(mis_mod, greek_coefficients = TRUE) +
  facet_wrap(~model_type, scales = "free")
```

![plot of chunk unnamed-chunk-8](figure/unnamed-chunk-8-1.png)

We could also add a different theme:


```r
plot(mis_mod, greek_coefficients = TRUE) +
  facet_wrap(~model_type, scales = "free") +
  theme_minimal()
```

![plot of chunk unnamed-chunk-9](figure/unnamed-chunk-9-1.png)
Any other changes through the `theme` function could also be done, for instance we could remove the legend since this isn't necessary when we have a faceted plot.


```r
plot(mis_mod, greek_coefficients = TRUE) +
  facet_wrap(~model_type, scales = "free") +
  theme(legend.position = "none")
```

![plot of chunk unnamed-chunk-10](figure/unnamed-chunk-10-1.png)
You could also change the font and font size here, plus many other options. Here are some ways you could modify the font in the facet header and axis title, using the `showtext` package for selecting a different font from Google fonts:

```r
library(showtext)
showtext_auto()
js <- "Josefin Sans"
font_add_google(js)

plot(mis_mod, greek = TRUE) +
  facet_wrap(~model_type, scales = "free") +
  theme(legend.position = "none",
        strip.text = element_text(family = js, size = 13),
        axis.title = element_text(js))
```

The plots can also be faceted by variable instead of model level:

```r
plot(mis_mod, greek_coefficients = TRUE) +
  facet_wrap(~variable, scales = "free", labeller = label_parsed) 
```

![plot of chunk unnamed-chunk-12](figure/unnamed-chunk-12-1.png)
In this case, that isn't terribly useful, but if you for instance have estimates from another model you would like to compare with inlamemi, you could join those results to `mis_plot$data` and then facet by variable (with method on the y-axis) to see the comparison clearly.

If you would like to add points or lines to the plot, this can also be done in an additional `geom` layer. For instance, since this data is simulated, I can add points at the numbers that were used for the simulation:

```r
mis_truth <- tibble::tribble(
  ~"variable", ~"value",
  "beta.0",  1,
  "beta.x",  2, 
  "beta.z1", 2, 
  "beta.z2", 2,
  "alpha.x.0",  1,
  "alpha.x.z1", 0.3, 
  "gamma.x.0", -1.5,
  "gamma.x.z2", -0.5,
  "gamma.x", 0
  )

plot(mis_mod) +
    geom_point(data = mis_truth, aes(x = value))
```

![plot of chunk unnamed-chunk-13](figure/unnamed-chunk-13-1.png)
Or we could add a vertical line at zero:


```r
plot(mis_mod) +
    geom_vline(xintercept = 0, linetype = "dotted")
```

![plot of chunk unnamed-chunk-14](figure/unnamed-chunk-14-1.png)







