---
title: "How to use [WIP]"
output: 
  rmarkdown::html_vignette:
    code_folding: hide
vignette: >
  %\VignetteIndexEntry{How to use [WIP]}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  warning = FALSE,
  code_folding = TRUE,
  comment = "#>"
)
```

The goal of the `profiplots` package is to unify graphics across analyses made by Profinit team. In this document we're introducing basic ways to do so. Take a look on the [plot gallery page](https://datascience.profinitservices.cz/sablony/profiplots-r/articles/plots-gallery.html), too.

```{r}
library(profiplots)
library(ggplot2)

sample_df <- data.frame(x=1:8, y=1:8, category=as.factor(LETTERS[1:8]))
```

## Set/Unset 

To ease the adaptation, there is a pair of functions to set up the most general option globaly. This affects both `baseR` and `ggplot2` graphics. 


* `profiplots::set_theme()` -- to set the profinit-look globaly. There are two params:
  * `pal_name` -- color palette to be used by default (ggplot: for continuous variables only).
  * `pal_name_discrete` -- color palette to be used for discrete mapping by default (ggplot only).
  * You can list available color palettes via `profiplots::profinit_pal.pals()`.
* `profiplots::unset_theme()` -- revert the settings.


::: {.panel-tabset .nav-pills group="viztype"}


### ggplot2

```{r, eval=TRUE, echo=TRUE, results="hold"}
plot_gg <- ggplot(sample_df, aes(x=x, y=y, fill=x)) + 
  stat_identity(geom="bar") + 
  theme_profinit()
plot_gg + labs(title = "ggplot - Default")

# turn on the settings
profiplots::set_theme("blue-red")  
plot_gg + labs(title = "ggplot - Profinit")

# turn off the settings
profiplots::unset_theme()
plot_gg + labs(title = "ggplot - Default again")
```

### base R

```{r eval=TRUE, echo=TRUE, results="hold"}
barplot(sample_df$x, col=sample_df$category, main = "Base R - Default", border = NA)

# turn on the settings
profiplots::set_theme("blue-red", "blue-red")  
barplot(sample_df$x, col=sample_df$category, main = "Base R - Profinit", border = NA)

# turn off the settings
profiplots::unset_theme()
barplot(sample_df$x, col=sample_df$category, main = "Base R - Default again", border = NA)
```


:::

## Using color scales

We're going to use `barplot` here. For examples of other plot types see the [plot gallery page](https://datascience.profinitservices.cz/sablony/profiplots-r/articles/plots-gallery.html). For demonstration purposes, we're going to tweak `fill` palette in ggplto2. Of course this can be applied to the color versions as well (`scale_color_profinit_c`, `scale_color_profinit_d`).


::: {.panel-tabset .nav-pills group="viztype"}

### ggplot2

#### Monochromatic color scale 

- Use either `reds`, `reds-dark`, `blues`, `blues-dark` or `greys`:

```{r}
plot_gg + 
  scale_fill_profinit_c(palette = "reds") +                             # HERE WE CHANGE THE PALETTE
  labs(title = "Example - monochromatic fill", fill = "Variable")

plot_gg + 
  scale_fill_profinit_c(palette = "blues-dark") +                       # HERE WE CHANGE THE PALETTE
  labs(title = "Example - monochromatic fill", fill = "Variable")
```

#### Gradient color scale 

- Use prespecified `blue-red` if possible. To create another gradient, you may use `scale_color_gradient`and `profinit_cols`.

```{r}
plot_gg + 
  scale_fill_profinit_c(palette = "blue-red") +                         # HERE WE CHANGE THE PALETTE
  labs(title = "Example - gradient fill", fill = "Variable")

plot_gg + 
  scale_fill_gradient(low = profinit_cols("red"), high = profinit_cols("yellow")) +  # HERE WE CHANGE THE PALETTE
  labs(title = "Example - gradient fill custom (NOT RECOMMEADED)", fill = "Variable")
```


#### Diverging color scale 

- Use prespecified `blue-white-red` palette. To create a new one, you can use `scale_color_gradient2` and `profinit_cols`:

```{r}
plot_gg + 
  scale_fill_profinit_c(palette = "blue-white-red", reverse = TRUE) +                         # HERE WE CHANGE THE PALETTE
  labs(title = "Example - diverging fill", fill = "Variable")

plot_gg + 
  scale_fill_gradient2(low = profinit_cols("red"), mid = "white", high = profinit_cols("pink"), midpoint = 4)                         # HERE WE CHANGE THE PALETTE
  labs(title = "Example - diverging fill (NOT RECOMMANDED)", fill = "Variable")
```

#### Discrete color palette 

- There are two discrete palettes, `discrete` (6 colors) and `discrete-full` (all colors specified in the Visual identity guidelines document). 
  * **Do not use** it this way, read the [Fundamentals of data vizualization](https://clauswilke.com/dataviz/) chapter on common use of color pitfalls.
  * To deal with too many levels -- you can extrapolate (set `exact` to `FALSE`)

```{r}
plot_gg + 
  aes(fill = as.character(x)) + 
  scale_fill_profinit_d(palette = "discrete") +                                # HERE WE CHANGE THE PALETTE
  labs(title = "Example - discrete fill (exact)", fill = "Variable")

plot_gg + 
  aes(fill = as.character(x)) + 
  scale_fill_profinit(palette = "discrete", exact = FALSE) +                   # HERE WE CHANGE THE PALETTE
  labs(title = "Example - discrete fill (interpolated)", fill = "Variable")

plot_gg + 
  aes(fill = as.character(x)) + 
  scale_fill_profinit_d(palette = "discrete-full") +                   # HERE WE CHANGE THE PALETTE
  labs(title = "Example - discrete fill (full, exact)", fill = "Variable")

```


### base R


#### Monochromatic color scale 

- Use either `reds`, `reds-dark`, `blues`, `blues-dark` or `greys`:

```{r}
barplot(
  height = sample_df$x, 
  names.arg = sample_df$category, 
  border = NA, 
  las = "1",                                  # rotate y-axis labels
  lwd.ticks = 1,
  bty = "7",
  col = profinit_pal("blue-red")(8), 
  main = "Base R - monochromatic fill"
)

barplot(
  height = sample_df$x, 
  names.arg = sample_df$category, 
  border = NA, 
   las = "1",                                  # rotate y-axis labels
  lwd.ticks = 1,
  bty = "]",
  col = profinit_pal("blues-dark")(8), 
  main = "Base R - monochromatic fill, another palette"
)
```

#### Gradient color scale 

- Use prespecified `blue-red` if possible. To create another gradient, you may use `scale_color_gradient`and `profinit_cols`.

```{r}
barplot(
  height = sample_df$x, 
  names.arg = sample_df$category, 
  border = NA, 
  col = profinit_pal("blue-red")(8),
  main = "Base R - gradient fill"
)

# This way you can define your own palette based on Profinit colors
red_yellow_pal <- grDevices::colorRampPalette(c(profinit_cols("red"), profinit_cols("yellow")))
barplot(
  height = sample_df$x, 
  names.arg = sample_df$category, 
  border = NA, 
  col = red_yellow_pal(8), 
  main = "Example - custom gradient fill (NOT RECOMMEADED)"
)
```


#### Diverging color scale 

- Use prespecified `blue-white-red` palette. To create a new one, you can use `scale_color_gradient2` and `profinit_cols`:

```{r}
barplot(
  height = sample_df$x, 
  names.arg = sample_df$category, 
  border = NA, 
  col = profinit_pal("blue-white-red")(8),
  main = "Base R - diverging fill"
)


# Create your own diverging palette based on Profinit colors
red_white_pink_pal <- grDevices::colorRampPalette(c(profinit_cols("red"), "white", profinit_cols("pink")))
barplot(
  height = sample_df$x, 
  names.arg = sample_df$category, 
  border = NA, 
  col = red_white_pink_pal(8),
  main = "Base R - custom diverging fill (NOT RECOMMANDED)"
)
```

#### Discrete color palette 

- There are two discrete palettes, `discrete` (6 colors) and `discrete-full` (all colors specified in the Visual identity guidelines document). 
  * **Do not use** it this way, read the [Fundamentals of data visualisation](https://clauswilke.com/dataviz/) chapter on common use of color pitfals.
  * To deal with too many levels -- you can extrapolate (set `exact` to `FALSE`)

```{r}
barplot(
  height = sample_df$x, 
  names.arg = sample_df$category, 
  border = NA, 
  col = profinit_pal("discrete")(8),
  main = "Base R - discrete fill (exact)"
)

barplot(
  height = sample_df$x, 
  names.arg = sample_df$category, 
  border = NA, 
  col = profinit_pal("discrete", exact = FALSE)(8),
  main = "Base R - discrete fill (interpolated, NOT RECOMMANDED)"
)


barplot(
  height = sample_df$x, 
  names.arg = sample_df$category, 
  border = NA, 
  col = profinit_pal("discrete-full")(8),
  main = "Base R - discrete fill (full, exact)"
)
```



:::


---

<details><summary>Session info</summary>
```{r}
sessionInfo()
```
</details>
