---
title: "Branding ggplots"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{ggplot-branding}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  out.width = "100%", 
  fig.width = 7, 
  fig.retina = 3
)
```

```{r setup}
library(lcbcr)
```

In the event of needing plots that have LCBC branding for presentations, reports etc. we have custom ggplot2 themes and colour scales to ensure that we have consistent branding. These are particularly suitable when using the rmarkdown-templates that also accompany this package.

## Applying the LCBC themes to ggplot

The themes are quite minimal, applying mostly just the current font and colours to match out branding.

```{r warning=FALSE, message = FALSE}
library(ggplot2)
library(dplyr)

p <- mtcars %>% 
  ggplot(aes(x = wt, y = disp, colour = cyl)) +
  geom_point()
p
```

```{r}
p + 
  theme_lcbc() +
  labs(title = "LCBC themes", 
       subtitle = "default")
```

There are variations around the main theme, to adapt to various possible scenarios and tastes, while maintaining a certain aesthetic

```{r}
p + theme_lcbc_grid() +
  labs(title = "LCBC themes", 
       subtitle = "grid")

p + theme_lcbc_dark() +
  labs(title = "LCBC themes", 
       subtitle = "dark")

p + theme_lcbc_void() +
  labs(title = "LCBC themes", 
       subtitle = "void")
```

and there are also some pre-defined combinations of the above also.

```{r}
p + theme_lcbc_dark_grid() +
  labs(title = "LCBC themes", 
       subtitle = "dark grid")

p + theme_lcbc_dark_void() +
  labs(title = "LCBC themes", 
       subtitle = "dark void")
```

## Colour scales

### LCBC scales

The colour scales work much the same way as standard ggplot scales do.
The main difference is you need to be aware if the data your are colouring is discrete (categorical) or continuous, to turn on or off the `discrete` argument.

```{r}
p + 
  theme_lcbc() +
  scale_colour_lcbc(discrete = FALSE) 

mtcars %>% 
  ggplot(aes(x = wt, y = disp, colour = factor(cyl))) +
  geom_point() +
  labs(title = "LCBC themes", 
       subtitle = "of various kinds") + 
  theme_lcbc() +
  scale_colour_lcbc(discrete = TRUE) 
```

By default, the scales grab the "main" palette, which consists of the blue and green colours of our logo. 
There are also other variations

```{r echo = FALSE}
lcbcr:::lcbc_palettes %>% 
  lapply(function(x) tibble(colour = x, r = 1:length(x))) %>% 
  bind_rows(.id="palette") %>% 
  mutate(palette = factor(palette, 
                          levels = names(lcbcr:::lcbc_palettes))
  ) %>% 
  ggplot(aes(x = r, y=palette, fill = I(colour))) +
  geom_tile() +
  labs(title = "LCBC palettes", x="") + 
  theme_lcbc()
```


```{r}
p + 
  theme_lcbc() +
  scale_colour_lcbc("dark", discrete = FALSE) 

p + 
  theme_lcbc_dark() +
  scale_colour_lcbc("light", discrete = FALSE) 

p + 
  theme_lcbc() +
  scale_colour_lcbc("blue", discrete = FALSE) 

p + 
  theme_lcbc() +
  scale_colour_lcbc("green", discrete = FALSE) 
```

## Adding logo to plots

Adding the LCBC logo to plots is a great way to brand images further, though should be used carefully, so it's not too garish.

```{r}
mtcars %>% 
  ggplot(aes(x = wt, y = disp, colour = cyl))+
  geom_point() +
  labs(title = "LCBC themes", 
       subtitle = "of various kinds") +
  scale_colour_lcbc(discrete = FALSE) +
  theme_lcbc() +
  add_lcbc_logo("text", alpha=.5) 
```

```{r}
mtcars %>% 
  ggplot(aes(x = wt, y = disp, colour = cyl)) +
  geom_point() +
  labs(title = "LCBC themes", 
       subtitle = "of various kinds")  +
  scale_colour_lcbc(discrete = FALSE) +
  theme_lcbc() +
  add_lcbc_logo(alpha=.7) 
```

You can control the size and position of the logos by using the `xmin`, `xmax`, `ymin`, `ymax` parameters, defining where on the plot it is placed. 


```{r}
mtcars %>% 
  ggplot(aes(x = wt, y = disp, colour = cyl)) +
  geom_point() +
  labs(title = "LCBC themes", 
       subtitle = "of various kinds")  +
  scale_colour_lcbc(discrete = FALSE) +
  theme_lcbc() +
  add_lcbc_logo(alpha = .7, 
                xmin = 4.5, xmax = Inf,
                ymin = 60 , ymax = 100
  ) 
```

```{r}
mtcars %>% 
  ggplot(aes(x = wt, y = disp, colour = cyl)) +
  geom_point() +
  labs(title = "LCBC themes", 
       subtitle = "of various kinds")  +
  scale_colour_lcbc(discrete = FALSE) +
  theme_lcbc() +
  add_lcbc_logo(type = "brain",
                alpha = .7, 
                xmin = 5, xmax = Inf,
                ymin = 60 , ymax = 110
  ) 
```

You can also add the logo outside the plot area, but that requires extra work, as ggplot automatically cuts away all data points that don't fit in the main plot area.

To coordinates can be found with some trial and error, here we want to place the logo at the bottom right. The plot y-axis starts at `50`, and the x-axis ends at `5.5`. SO here we specify that the logo should be placed below the y-axis and to the right of the x-axis, extending the main plot area beyond the data.

To keep whatever we place there, we need to tell ggplot to _not_ `clip` anything outside the main plot out, turning clipping off with `coord_cartesian`.

After this, we need to tell ggplot to extend the plot to include the area we placed the logo. This is trial and error, changing the numbers in `units` changes how much the plot is extended to each of it's four sides. `1` is no extension, and any number above or below that will increase or reduce the plotting area, respectively.

```{r}
mtcars %>% 
  ggplot(aes(x = wt, y = disp, colour = cyl)) +
  geom_point() +
  labs(title = "LCBC themes", 
       subtitle = "of various kinds")  +
  scale_colour_lcbc(discrete = FALSE) +
  theme_lcbc() +
  add_lcbc_logo(alpha = 1, 
                xmin = 4.7, xmax = 6.7,
                ymin = -10 , ymax = 30
  ) +
  coord_cartesian(clip = "off") +
  theme(plot.margin = unit(c(1, 2, 3, 1), "lines"))
```
