-
Notifications
You must be signed in to change notification settings - Fork 0
/
ggplot-branding.Rmd
executable file
·199 lines (159 loc) · 5.53 KB
/
ggplot-branding.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
---
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 = 10,
fig.retina = TRUE
)
devtools::load_all(".")
```
```{r setup, include=FALSE}
library(MOAS)
```
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 corrent 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() +
labs(title = "LCBC themes",
subtitle = "of various kinds")
p +
theme_lcbc()
```
There are variations around the main theme, to adapt to various possible scenarios and tastes, while maintaing a certain aesthetic
```{r}
p + theme_lcbc_grid()
p + theme_lcbc_dark()
p + theme_lcbc_void()
```
and there are also some pre-defined combinations of the above also.
```{r}
p + theme_lcbc_dark_grid()
p + theme_lcbc_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}
MOAS:::lcbc_palettes %>%
lapply(function(x) tibble(colour = x, r = 1:length(x))) %>%
bind_rows(.id="palette") %>%
mutate(palette = factor(palette,
levels = names(MOAS:::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("long", 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 datapoints that dont 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 beyong 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 extention, 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"))
```