---
title: "Maraca Plots - Plotting win odds"
author: "Monika Huhn"
date: "10/10/2023"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Maraca Plots - Plotting win odds}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

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

## Component plot

The maraca package also contains an additional plot
called `"component_plot"`. This one allows to plot the
different components that make up the win odds calculation.
More specifically, for each outcome, the plot shows how
often patients in each treatment arm "won" against the other
arm. For the time-to-event endpoints, this means counting how many
patients of the other arm had no more prioritized event prior.
For the continuous outcome this means counting how many patients had a lower value.
The results are separated for each outcome (non-cumulative)
and also include ties (patients from 2 treatment arms having same
outcome at the same time/same continuous outcome value).

Let us first read in some data.
```{r maraca1, eval = TRUE}
library(maraca)

data(hce_scenario_a)
```

In order to use the `component_plot`, we have to first create a
`maraca` object. Important here is to set the argument
`compute_win_odds = TRUE`, so that the necessary calculations
are included.
```{r}
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"),
  # Make sure to calculate the win odds
  compute_win_odds = TRUE
)
```

Now we can just plot the object using the `component_plot()` function.
```{r fig.width=7, fig.height=6}
component_plot(maraca_dat)
```

It is also possible to use the `component_plot()` function directly on
an `hce` object (created using the
[hce package](https://cran.r-project.org/package=hce)).

```{r fig.width=7, fig.height=6}
library(hce)

Rates_A <- c(1.72, 1.74, 0.58, 1.5, 1)
Rates_P <- c(2.47, 2.24, 2.9, 4, 6)

hce_dat <- simHCE(n = 2500, TTE_A = Rates_A, TTE_P = Rates_P,
                  CM_A = -3, CM_P = -6, CSD_A = 16, CSD_P = 15, fixedfy = 3,
                  seed = 31337)

component_plot(hce_dat)
```

## Cumulative plot

Furthermore, there is a plot called `"cumulative_plot"`.
Similar to the `component_plot`, this plot shows the different HCE components that
make up the win odds calculation. Different to the component plot,
this plot provides insight into the contributed effect for each of the components as
they are added in sequence (from top to bottom).
Additionally, there is also a right-hand panel that shows a forest plot with the win odds
and win ratio corresponding to the same cumulative sequence. To understand the contribution
from each outcome, we artificially set all the less prioritized outcomes as ties and calculate
the win odds/ratio. Thus, for each added outcome there will be less ties.  

As before, in order to use the `cumulative_plot`, we have to first create a
`maraca` object. Important here is to set the argument
`compute_win_odds = TRUE`, so that the necessary calculations
are included.
```{r}
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"),
  # Make sure to calculate the win odds
  compute_win_odds = TRUE
)
```

Now we can just plot the object using the `cumulative_plot()` function.
```{r fig.width=7, fig.height=6}
cumulative_plot(maraca_dat)
```

It is also possible to use the `cumulative_plot()` function directly on
an `hce` object (created using the
[hce package](https://cran.r-project.org/package=hce)).

```{r fig.width=7, fig.height=6}
cumulative_plot(hce_dat)
```

The user can also choose to only display one of the statistics (win odds or win ratio)
by specifying so in the `include` parameter.
```{r fig.width=7, fig.height=6}
cumulative_plot(maraca_dat, include = "win odds")
```

The y-axis can easily be reversed using the `reverse` parameter.
```{r fig.width=7, fig.height=6}
cumulative_plot(hce_dat, reverse = TRUE)
```

## Styling

The resulting plot for the `component_plot()` functions
is a normal ggplot2 object that can be styled accordingly.
```{r fig.width=7, fig.height=6}
component_plot(maraca_dat) +
  ggplot2::scale_fill_manual(values = c("seagreen", "red", "grey"), name = NULL)
```

Note that the `cumulative_plot()` function is using the
patchwork package to combine 2 ggplot2 objects - the
bar plot and the forest plot that together make up the
`cumulative_plot()`. They
can be accessed as list items and styled accordingly.
```{r fig.width=7, fig.height=6}
p <- cumulative_plot(maraca_dat)
# Accessing the first ggplot2 object and adding styling (bar plot)
p[[1]] <- p[[1]] +
  ggplot2::scale_fill_manual(values = c("seagreen", "red", "grey"), name = NULL)
p
```

For the users convenience, there are also different themes
available to style the plot.

The default style is called `theme = "maraca"`.
```{r fig.width=7, fig.height=6}
component_plot(maraca_dat, theme = "maraca")
```

There are 2 different themes with different color
schemes, `theme = "color1"` and `theme = "color2"`.
```{r fig.width=7, fig.height=6}
cumulative_plot(maraca_dat, theme = "color1")
```

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

There is also a theme without any styling `theme = "none"` that
can be used as a base when the user wants to style the plot themselves.
```{r fig.width=8, fig.height=6}
cumulative_plot(maraca_dat, theme = "none")
```