-
Notifications
You must be signed in to change notification settings - Fork 108
/
README.Rmd
370 lines (262 loc) · 10.5 KB
/
README.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
---
output:
md_document:
variant: markdown_github
toc: true
toc_depth: 2
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r echo=FALSE}
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(gridExtra))
example_plot <- ggplot(diamonds, aes(price, fill = factor(cut))) +
geom_histogram(binwidth = 850) +
xlab('Price (USD)') +
ylab('Count') +
scale_x_continuous(label = function(x) paste0(x / 1000, 'k')) +
theme(legend.position = 'none') +
scale_y_continuous(label = function(x) format(x, big.mark = ",", scientific = FALSE))
library(ggthemr)
source('scripts/preview_theme.R')
source('scripts/preview_layout.R')
```
ggthemr
========================================================
Themes for ggplot2. The idea of this package is that you can just set the theme and then forget about it. You shouldn't have to change any of your existing code. There are several parts to a theme:
* Colour palette for the background, axes, gridlines, text etc.
* Layout of axes lines and gridlines.
* Spacing around plot and between elements (i.e. axes titles to axes lines etc). You can set the spacing to determine how compact or spread out a plot is.
* Text size.
There are a number of preset palettes and layouts, and methods to create your own colour schemes.
Installation
-------------------------
This package is still under development, but can be installed using [devtools](http://cran.r-project.org/web/packages/devtools/index.html).
```{r eval=FALSE}
devtools::install_github('cttobin/ggthemr')
```
Usage
-------------------------
To just set the colour scheme:
```{r eval=FALSE}
ggthemr('dust')
```
That's it. Any ggplot you create from then on will have the theme applied. You can clear the theme and return to ggplot2's default using:
```{r eval=FALSE}
ggthemr_reset()
```
Palettes
-------------------------
The palette determines the colours of everything in a plot including the background, layers, gridlines, title text, axes lines, axes text and axes titles. The *swatch* is the the name given to the set of colours strictly used in styling the geoms/layer elements (e.g. the points in `geom_point()`, bars in `geom_bar()` etc.). At least six colours have been supplied in each palette's swatch.
There are a wide variety of themes in this package (and more on the way). Some of them serious business... others are deliberately stylish and might not be that good for use in proper publications.
### flat
[Base 16](https://github.com/chriskempson/base16)
```{r fig.width=10, fig.height=6, echo=FALSE}
ggthemr('flat', spacing = 0.5, type = 'inner')
preview_theme()
```
### flat dark
[Base 16](https://github.com/chriskempson/base16)
```{r fig.width=10, fig.height=6, echo=FALSE}
ggthemr('flat dark', spacing = 0.5, type = 'inner')
preview_theme()
```
### camouflage
```{r fig.width=10, fig.height=6, echo=FALSE}
ggthemr('camouflage', spacing = 0.5, type = 'inner')
preview_theme()
```
### chalk
```{r fig.width=10, fig.height=6, echo=FALSE}
ggthemr('chalk', spacing = 0.5, type = 'inner')
preview_theme()
```
### copper
```{r fig.width=10, fig.height=6, echo=FALSE}
ggthemr('copper', spacing = 0.5, type = 'inner')
preview_theme()
```
### dust
```{r fig.width=10, fig.height=6, echo=FALSE}
ggthemr('dust', spacing = 0.5, type = 'inner')
preview_theme()
```
### earth
```{r fig.width=10, fig.height=6, echo=FALSE}
ggthemr('earth', spacing = 0.5, type = 'inner')
preview_theme()
```
### fresh
```{r fig.width=10, fig.height=6, echo=FALSE}
ggthemr('fresh', spacing = 0.5, type = 'inner')
preview_theme()
```
### grape
```{r fig.width=10, fig.height=6, echo=FALSE}
ggthemr('grape', spacing = 0.5, type = 'inner')
preview_theme()
```
### grass
```{r fig.width=10, fig.height=6, echo=FALSE}
ggthemr('grass', spacing = 0.5, type = 'inner')
preview_theme()
```
### greyscale
```{r fig.width=10, fig.height=6, echo=FALSE}
ggthemr('greyscale', spacing = 0.5, type = 'inner')
preview_theme()
```
### light
```{r fig.width=10, fig.height=6, echo=FALSE}
ggthemr('light', spacing = 0.5, type = 'inner')
preview_theme()
```
### lilac
```{r fig.width=10, fig.height=6, echo=FALSE}
ggthemr('lilac', spacing = 0.5, type = 'inner')
preview_theme()
```
### pale
```{r fig.width=10, fig.height=6, echo=FALSE}
ggthemr('pale', spacing = 0.5, type = 'inner')
preview_theme()
```
### sea
```{r fig.width=10, fig.height=6, echo=FALSE}
ggthemr('sea', spacing = 0.5, type = 'inner')
preview_theme()
```
### sky
```{r fig.width=10, fig.height=6, echo=FALSE}
ggthemr('sky', spacing = 0.5, type = 'inner')
preview_theme()
```
### solarized
```{r fig.width=10, fig.height=6, echo=FALSE}
ggthemr('solarized', spacing = 0.5, type = 'inner')
preview_theme()
```
Custom Palettes
-------------------------
`define_palette()` lets you make your own themes that can be passed to `ggthemr()` just like any of the palettes above. Here's an example of a (probably ugly) palette using random colours:
``` {r fig.width=5.5, fig.height=4.5}
# Random colours that aren't white.
set.seed(12345)
random_colours <- sample(colors()[-c(1, 253, 361)], 10L)
ugly <- define_palette(
swatch = random_colours,
gradient = c(lower = random_colours[1L], upper = random_colours[2L])
)
ggthemr(ugly)
example_plot + ggtitle(':(')
```
You can define all elements of a palette using `define_palette()` including colours for the background, text, axes lines, swatch and gradients.
Layouts
-------------------------
The layout of a theme controls the appearance and position of the axes, gridlines and text. Some folk prefer both major and minor gridlines, others prefer none or something in between.
### Clean
```{r fig.width=5.5, fig.height=3.5, echo=FALSE}
ggthemr('grape', layout = 'clean', spacing = 0.5)
preview_layout()
```
### Clear (default)
```{r fig.width=5.5, fig.height=3.5, echo=FALSE}
ggthemr('grape', layout = 'clear', spacing = 0.5)
preview_layout()
```
### Minimal
```{r fig.width=5.5, fig.height=3.5, echo=FALSE}
ggthemr('grape', layout = 'minimal', spacing = 0.5)
preview_layout()
```
### Plain
```{r fig.width=5.5, fig.height=3.5, echo=FALSE}
ggthemr('grape', layout = 'plain', spacing = 0.5)
preview_layout()
```
### Scientific
```{r fig.width=5.5, fig.height=3.5, echo=FALSE}
ggthemr('grape', layout = 'scientific', spacing = 0.5)
preview_layout()
```
Spacing
-------------------------
Plot margins and space between axes titles and lines etc. is controlled with the *spacing* parameter. Lower values will make plots more compact, higher values will give them more padding. Compare the plots below where the spacing has been set to 0, 1 and 2 respectively.
```{r fig.width=5.5, fig.height=3.5, echo=FALSE}
ggthemr('earth', spacing = 0, type = 'outer')
preview_layout()
```
```{r fig.width=5.5, fig.height=3.5, echo=FALSE}
ggthemr('earth', spacing = 1, type = 'outer')
preview_layout()
```
```{r fig.width=5.5, fig.height=3.5, echo=FALSE}
ggthemr('earth', spacing = 2, type = 'outer')
preview_layout()
```
Type
-------------------------
The *type* parameter can be set to either *inner* or *outer*. When *inner*, the background colour of a plot will not extend past the plot area. *outer* will colour the entire plot and background.
```{r fig.width=5.5, fig.height=3.5, echo=TRUE}
ggthemr('earth', type = 'inner')
example_plot
```
```{r fig.width=5.5, fig.height=3.5, echo=TRUE}
ggthemr('earth', type = 'outer')
example_plot
```
Tweaking Themes
-------------------------
Squinting at a chart? Low on printer ink? ggthemr includes some methods to tweak charts to make them lighter or darker. Here's a standard theme:
```{r fig.width=7, fig.height=6, echo=TRUE}
ggthemr('dust')
example_plot
```
Maybe that plot comes out a bit pale looking when you print it. Here's how you can add a bit more contrast to the swatch:
```{r fig.width=7, fig.height=6, echo=TRUE}
darken_swatch(amount = 0.3)
example_plot
```
The second parameter to `darken_swatch()` controls the degree to which the colours are made darker. Full list of methods with similar functionality:
* `darken_swatch()` / `lighten_swatch()`: darker/lighter swatch colours.
* `darken_gradient()` / `lighten_gradient()`: darker/lighter gradient colours.
* `darken_palette()` / `lighten_palette()`: darker/lighter everything.
I'll add methods to darken/lighten the axes lines and text soon too.
Plot Adjustments
-------------------------
Most of the time you'll probably just want to set the theme and not worry about it. There may be times though where you'll want to make some small adjustment, or manually change what items appear as what colour in a plot.
```{r fig.width=7, fig.height=6, echo=TRUE}
ggthemr('dust')
mpg_plot <- ggplot(mpg[mpg$drv != '4', ], aes(factor(cyl), cty, fill = drv)) +
geom_boxplot() + labs(x = 'Cylinders', y = 'City MPG', fill = 'Drive Type') +
theme(legend.position = 'bottom')
mpg_plot
```
For some reason you decide you want to change those colours. Front-wheel drive vehicles should be orange. Rear-wheelers should be that red colour. You could change the order of the levels of your fill variable, but you shouldn't have to do that. You just want to switch those colours but you have no idea what they are. `swatch()` will give you the colours in the currently active ggthemr palette.
```{r}
swatch()
```
So you can manually swap the two colours around.
```{r fig.width=7, fig.height=6, echo=TRUE}
to_swap <- swatch()[2:3]
mpg_plot + scale_fill_manual(values = rev(to_swap))
```
**Note:** the first colour in a swatch is a special one. It is reserved for outlining boxplots, text etc. So that's why the second and third colours were swapped.
A note about theme setting
--------------------------
ggthemr does three different things while setting a theme.
1. It updates the default ggplot2 theme with the specified ggthemr theme by using the `ggplot2::theme_set()` function.
2. It modifies the aesthetic defaults for all geoms using the `ggplot2::update_geom_defaults()` function.
3. It creates functions for all the different scales in the global environment.
In case, if you do not want to set the theme this way, use the `set_theme = FALSE` option while using the `ggthemr` function. An example of setting theme, geom aesthetic defaults and scales manually:
```{r}
ggthemr_reset()
dust_theme <- ggthemr('dust', set_theme = FALSE)
example_plot
example_plot + dust_theme$theme
example_plot + dust_theme$theme + dust_theme$scales$scale_fill_discrete()
do.call(what = ggplot2::update_geom_defaults, args = dust_theme$geom_defaults$new$bar)
ggplot(diamonds, aes(price)) + geom_histogram(binwidth = 850) + dust_theme$theme
```
License
-------
Released under GPL-3.