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

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 6, 
  fig.height = 6,
  warning=FALSE
)
```

```{r setup}
library(ThinkingGrid)
library(lme4)
library(sjPlot)
library(png)
library(grid)

data_file <- system.file("extdata", "sample_data.csv", package = "ThinkingGrid")
tg_data <- read.csv(data_file)

knitr::kable(head(tg_data))
```

In case data is already collected, users can automatically add the quadrant depths using the `add_depths` function. The deliberate and automatic constraints column names must be passed to the function and must range from one to six for it to work correctly. 

```{r add_depths}
tg_data <- add_depths(
  tg_data,
  dc = "dc",
  ac = "ac"
)

knitr::kable(head(tg_data))
```

The depths can be used as dependent variables in statistical analyses. The `thinkgrid_quadrant_plot` function provides a convenient way to visualize each of these analyses within their individual quadrants. Build the models and their respective ggplot2 objects similar to the example shown below.

```{r models_and_plots}
free_model <- lmer(free ~ valence + (1|id), data = tg_data)
sticky_model <- lmer(sticky ~ valence + (1|id), data = tg_data)
directed_model <- lmer(directed ~ valence + (1|id), data = tg_data)
salience_model <- lmer(hybrid ~ valence + (1|id), data = tg_data)

free_plot <- plot_model(free_model, type = "pred")
sticky_plot <- plot_model(sticky_model, type = "pred")
directed_plot <- plot_model(directed_model, type = "pred")
salience_plot <- plot_model(salience_model, type = "pred")
```

Once the plots are made, simply pass them to the `thinkgrid_quadrant_plot` function.

```{r thinkgrid_quadrant_plot1}
combined_plot1 <- thinkgrid_quadrant_plot(
  p_sticky = sticky_plot,
  p_salience = salience_plot,
  p_free = free_plot,
  p_directed = directed_plot
)

combined_plot1
```

This function also accepts images as arguments, as long as they are transparent (e.g., png). Instead of passing the ggplot2 objects, the png object can be passed instead.

```{r thinkgrid_quadrant_plot2}
image_file <- system.file("extdata", "rabbiduck.png", package = "ThinkingGrid")
image <- readPNG(image_file)
image <- rasterGrob(image)

combined_plot2 <- thinkgrid_quadrant_plot(
  p_sticky = image,
  p_salience = salience_plot,
  p_free = image,
  p_directed = directed_plot
)

combined_plot2
```
<br>

# Plotting Quirks

You will notice that the theme of the argument plots is different than the theme of the
subplots. This is because, by default, the inner theme (i.e., subplot theme) is generated by
```{r inner_theme}
default_inner_theme
```

You can manually set the inner theme using the `inner_theme` optional argument. This will not
affect png images, only the plots.
```{r thinkgrid_plot_with_theme}
library(ggplot2)

thinkgrid_quadrant_plot(
  p_sticky = image,
  p_salience = salience_plot,
  p_free = image,
  p_directed = directed_plot,
  inner_theme = ggplot2::theme_dark()
)


```

## Rendering
If you are using RStudio, plots generated by `thinkgrid_quadrant_plot` have
a tendency to not render especially nicely (funny aspect ratios, text crowding issues, etc).
To solve such rendering issues, we recommend saving the plot to a file.
```{r, eval = FALSE}
ggplot2::ggsave("properly_rendered.jpg", combined_plot2, width = 10, height = 10, dpi = 300)
```

<br>
<br>
<br>
