---
title: "Maraca Plots - Themes and Styling"
author: "Monika Huhn"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Maraca Plots - Themes and Styling}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---


```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE, collapse = TRUE)
library(dplyr)
library(ggplot2)
library(maraca)
```

In order to give the user more options in styling their plot,
we have added a theme argument to the plotting functions.
This vignette gives an overview over the different options.

Let us first read in some data and prepare for plotting:
```{r maraca1, eval = TRUE}
library(maraca)

data(hce_scenario_a)

maraca_dat <- maraca(
  data = hce_scenario_a,
  step_outcomes = c("Outcome I", "Outcome II", "Outcome III", "Outcome IV"),
  last_outcome = "Continuous outcome",
  fixed_followup_days = 3 * 365,
  column_names = c(outcome = "GROUP", arm = "TRTP", value = "AVAL0"),
  arm_levels = c(active = "Active", control = "Control"),
  compute_win_odds = TRUE
)
```

## Default themes

By default, the plotting functions are using a theme called
"maraca". It is implicitly called so you do not have to specify
the argument `theme = "maraca"`.

```{r fig.width = 8, fig.height = 6}
plot(maraca_dat)
```

The default maraca theme adds an x- and y-axis label and angels the
x-axis text for better readability. It also uses the theme "bw"
of the ggplot2 package.
The plot colors are not styled in the default maraca theme. We include
2 different color themes by default for the users convenience,
`theme = "color1"` and `theme = "color2"`.

```{r fig.width = 8, fig.height = 6}
plot(maraca_dat, theme = "color1")
```

```{r fig.width = 8, fig.height = 6}
plot(maraca_dat, theme = "color2")
```

We kept the previous default styling of the maraca package as its
own theme, `theme = "maraca_old"`

```{r fig.width = 8, fig.height = 6}
plot(maraca_dat, theme = "maraca_old")
```

If you want to completely style your code by yourself, remove any
of the extra styling with `theme = "none"`.

```{r fig.width = 8, fig.height = 6}
plot(maraca_dat, theme = "none")
```


## Extending themes

The maraca plot is an ggplot2 object that can be styled by
adding extra styling layers, such as any ggplot2 plot.

For example, using the `theme = "none"` styling, we can add
manually the angle of the x-axis labels, remove axis texts
and style with differnt colors.

```{r fig.width = 8, fig.height = 6}
library(ggplot2)

p <- plot(maraca_dat, theme = "none")

p <- p +
  theme(
    legend.position = "bottom",
    axis.text.x.bottom = element_text(
      angle = 90,
      vjust = 0.5,
      hjust = 1
    ),
    axis.title.x = element_blank(),
    axis.title.y = element_blank()
  ) +
  scale_color_viridis_d(end = 0.8) +
  scale_fill_viridis_d(end = 0.8)

p
```

If you want to use your own color scheme on the plots,
the easiest way is to use the default styling `theme = "maraca"`
and then add the colors as an extra layer.

```{r fig.width = 8, fig.height = 6}
p <- plot(maraca_dat)

colorScheme <- c("Active" = "steelblue", "Control" = "seagreen3")

p <- p +
  scale_color_manual(values = colorScheme) +
  scale_fill_manual(values = colorScheme)

p
```

Note that the numeric x-axis labels for the continuous endpoint part of the plot
are actually text labels on the plot and cannot be directly changed using the
ggplot2 `theme()` function.

The user has 2 ways of changing these labels. One is to overwrite the default
text size for the plot. Do not forget to reset the default text size afterwards
for further plotting.

```{r fig.width = 8, fig.height = 6}
keep_default <- GeomLabel$default_aes$size

# Changing default text size for the plot
update_geom_defaults("text", list(size = 3))

plot(maraca_dat)

# Make sure to change defaults back to default for new plots
update_geom_defaults("text", list(size = keep_default))
```

Alternatively, the user can use the parameter `continuous_grid_spacing_x` in the `plot()`
function for maraca objects to create more or less grid lines and labels.

```{r fig.width = 8, fig.height = 6}
plot(maraca_dat, continuous_grid_spacing_x = 20)
```




