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

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  message = FALSE,
  warning = FALSE
  # dev = "png", # uncomment if issues running locally on mac
  # dev.args = list(type = "cairo-png")
)
```

plotBart uses [ggplot2](https://ggplot2.tidyverse.org/) to construct plots. These functions return ggplot objects that can be manipulated similar to standard `ggplot2::ggplot()` calls. Plot titles, subtitles, labels, captions, and themes can be easily customized using ggplot syntax.

```{r setup}
library(plotBart)
data(lalonde)
confounders <- c('age', 'educ', 'black', 'hisp', 'married', 'nodegr')

# plot balance across treatment and control groups
p <- plot_balance(.data = lalonde,
                  treatment = 'treat',
                  confounders = confounders)
p
```

<br>

### Modify the plot labels:

```{r}
p +
  labs(title = 'My comments on the results',
       subtitle = NULL,
       caption = 'December 2021',
       x = 'Mean diff b/t treatment and control')
```

<br>

### Or change the theme entirely:

```{r}
p + 
  theme_classic()
# set the theme for all plots within this R session
theme_set(theme_bw())
```
<br>

### Extract data to reconstruct the plot:

```{r}
p$data
```
<br>

### Refer to the ggplot2 documentation for more details:
- [ggplot2 cheatsheet](https://ggplot2.tidyverse.org/)
- [Labels](https://ggplot2.tidyverse.org/reference/labs.html)
- [Complete themes](https://ggplot2.tidyverse.org/reference/ggtheme.html)
- [Modify a theme](https://ggplot2.tidyverse.org/reference/theme.html)
