forked from Mikata-Project/ggthemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.markdown_github
306 lines (199 loc) · 9.36 KB
/
README.markdown_github
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
- [ggthemr](#ggthemr)
- [Installation](#installation)
- [Usage](#usage)
- [Further Development](#further-development)
- [Palettes](#palettes)
- [flat](#flat)
- [flat dark](#flat-dark)
- [camoflauge](#camoflauge)
- [chalk](#chalk)
- [copper](#copper)
- [dust](#dust)
- [earth](#earth)
- [fresh](#fresh)
- [grape](#grape)
- [grass](#grass)
- [greyscale](#greyscale)
- [light](#light)
- [lilac](#lilac)
- [pale](#pale)
- [sea](#sea)
- [sky](#sky)
- [solarized](#solarized)
- [Custom Palettes](#custom-palettes)
- [Layouts](#layouts)
- [Clean](#clean)
- [Clear (default)](#clear-default)
- [Minimal](#minimal)
- [Plain](#plain)
- [Scientific](#scientific)
- [Spacing](#spacing)
- [Type](#type)
- [Tweaking Themes](#tweaking-themes)
- [Plot Adjustments](#plot-adjustments)
<!-- README.md is generated from README.Rmd. Please edit that file -->
## Error in library(gridExtra): there is no package called 'gridExtra'
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 and isn't too stable yet, but can be installed using [devtools](http://cran.r-project.org/web/packages/devtools/index.html).
``` r
devtools::install_github('cttobin/ggthemr')
```
Usage
-----
To just set the colour scheme:
``` r
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
ggthemr_reset()
```
Further Development
-------------------
Features on the way:
- More palettes and layouts.
- Options to control fonts.
- Ability to reset themes back to default.
- Theme modifiers (so you can do things like `plot + no_legend()`, `plot + no_axes()` etc.).
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 delibrately stylish and might not be that good for use in proper publications.
### flat
[Base 16](https://github.com/chriskempson/base16)
## Error in do.call(grid.arrange, display): object 'grid.arrange' not found
### flat dark
[Base 16](https://github.com/chriskempson/base16)
## Error in do.call(grid.arrange, display): object 'grid.arrange' not found
### camoflauge
## Error in do.call(grid.arrange, display): object 'grid.arrange' not found
### chalk
## Error in do.call(grid.arrange, display): object 'grid.arrange' not found
### copper
## Error in do.call(grid.arrange, display): object 'grid.arrange' not found
### dust
## Error in do.call(grid.arrange, display): object 'grid.arrange' not found
### earth
## Error in do.call(grid.arrange, display): object 'grid.arrange' not found
### fresh
## Error in do.call(grid.arrange, display): object 'grid.arrange' not found
### grape
## Error in do.call(grid.arrange, display): object 'grid.arrange' not found
### grass
## Error in do.call(grid.arrange, display): object 'grid.arrange' not found
### greyscale
## Error in do.call(grid.arrange, display): object 'grid.arrange' not found
### light
## Error in do.call(grid.arrange, display): object 'grid.arrange' not found
### lilac
## Error in do.call(grid.arrange, display): object 'grid.arrange' not found
### pale
## Error in do.call(grid.arrange, display): object 'grid.arrange' not found
### sea
## Error in do.call(grid.arrange, display): object 'grid.arrange' not found
### sky
## Error in do.call(grid.arrange, display): object 'grid.arrange' not found
### solarized
## Error in do.call(grid.arrange, display): object 'grid.arrange' not found
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
# 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(':(')
```
![plot of chunk unnamed-chunk-22](figure/unnamed-chunk-22-1.png)
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
![plot of chunk unnamed-chunk-23](figure/unnamed-chunk-23-1.png)
### Clear (default)
![plot of chunk unnamed-chunk-24](figure/unnamed-chunk-24-1.png)
### Minimal
![plot of chunk unnamed-chunk-25](figure/unnamed-chunk-25-1.png)
### Plain
![plot of chunk unnamed-chunk-26](figure/unnamed-chunk-26-1.png)
### Scientific
![plot of chunk unnamed-chunk-27](figure/unnamed-chunk-27-1.png)
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.
![plot of chunk unnamed-chunk-28](figure/unnamed-chunk-28-1.png)
![plot of chunk unnamed-chunk-29](figure/unnamed-chunk-29-1.png)
![plot of chunk unnamed-chunk-30](figure/unnamed-chunk-30-1.png)
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
ggthemr('earth', type = 'inner')
example_plot
```
![plot of chunk unnamed-chunk-31](figure/unnamed-chunk-31-1.png)
``` r
ggthemr('earth', type = 'outer')
example_plot
```
![plot of chunk unnamed-chunk-32](figure/unnamed-chunk-32-1.png)
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
ggthemr('dust')
example_plot
```
![plot of chunk unnamed-chunk-33](figure/unnamed-chunk-33-1.png)
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
darken_swatch(amount = 0.3)
example_plot
```
![plot of chunk unnamed-chunk-34](figure/unnamed-chunk-34-1.png)
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
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
```
![plot of chunk unnamed-chunk-35](figure/unnamed-chunk-35-1.png)
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()
```
## [1] "#555555" "#db735c" "#EFA86E" "#9A8A76" "#F3C57B" "#7A6752" "#2A91A2"
## [8] "#87F28A" "#6EDCEF"
## attr(,"class")
## [1] "ggthemr_swatch"
So you can manually swap the two colours around.
``` r
to_swap <- swatch()[2:3]
mpg_plot + scale_fill_manual(values = rev(to_swap))
```
![plot of chunk unnamed-chunk-37](figure/unnamed-chunk-37-1.png)
**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.