-
Notifications
You must be signed in to change notification settings - Fork 0
/
workbook.rmd
261 lines (243 loc) · 10.6 KB
/
workbook.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
---
title: "Dataviz with ggplot2::theme()"
date: "2022-09-12"
output: html_document
---
# Resources
- colour palette picker + colour-blind simulator: coolors.co
- more on themes: https://ggplot2-book.org/polishing.html
- fonts with `sysfonts`: https://stackoverflow.com/questions/71573377/cannot-import-fonts-into-r
- fonts with `extrafont`: https://uribo.github.io/rpkg_showcase/design/extrafont.html
# Workbook
```{r}
#load libraries
library(tidyverse)
library(bbplot)
#fonts
library(extrafont)
library(sysfonts)
library(showtext)
```
```{r}
#load data
spp <- read.csv("data/spp_df.csv")[-c(1)]
head(spp)
```
```{r}
#base plot that we are working with to build on
ggplot(data = na.omit(spp), #don't plot NA values
aes(x = y,
y = log10(publications), #normalise distribution
colour = order)) +
geom_point()
```
```{r}
#some filtering and cleaning up the labels
ggplot(data = na.omit(spp),
aes(x = y,
y = log10(publications),
colour = order)) +
geom_point() +
labs(x = "Latitude (median)",
y = "Number of publications",
colour = "Order") +
#adding degree symbol and locking in tick marks so that they don't move around when we make changes
scale_x_continuous(breaks = c(-40, 0, 40, 80),
labels = c("-40°", 0, "40°", "80°")) +
#changing values after normalising distribution
scale_y_continuous(breaks = c(0, 1, 2, 3, 4),
labels = c(1, 10, 100, "1,000", "10,000"))
```
```{r}
#changing colours & customising geom_point
ggplot(data = na.omit(spp),
aes(x = y,
y = log10(publications),
colour = order)) +
geom_point(size = 3,
alpha = 0.4) +
labs(x = "Latitude (median)",
y = "Number of publications",
colour = "Order") +
scale_x_continuous(breaks = c(-40, 0, 40, 80),
labels = c("-40°", 0, "40°", "80°")) +
scale_y_continuous(breaks = c(0, 1, 2, 3, 4),
labels = c(1, 10, 100, "1,000", "10,000")) +
#colour palette
scale_colour_manual(values = c("#FAEBB3", "#F7E28D", "#F6DB6E", "#F4D34E", "#F3CC2F",
"#f1c40f", "#EFB519", "#EDA722", "#EB982C", "#E98935",
"#E97A37", "#E86B39", "#E85B3A", "#e74c3c", "#D14A58",
"#BB4875", "#A44691", "#8e44ad", "#7859B9", "#616EC4",
"#4B83D0", "#3498db", "#33A5C1", "#31B2A6", "#30BF8C",
"#2ECC71", "#4ED487", "#6EDC9D", "#8DE3B2", "#8CE3B2"),
#customise the look of legend
guide = guide_legend(override.aes = list(size = 6,
alpha = 1)))
```
```{r}
#apply some pre-built themes
ggplot(data = na.omit(spp),
aes(x = y,
y = log10(publications),
colour = order)) +
geom_point(size = 3,
alpha = 0.4) +
labs(x = "Latitude (median)",
y = "Number of publications",
colour = "Order") +
scale_x_continuous(breaks = c(-40, 0, 40, 80),
labels = c("-40°", 0, "40°", "80°")) +
scale_y_continuous(breaks = c(0, 1, 2, 3, 4),
labels = c(1, 10, 100, "1,000", "10,000")) +
scale_colour_manual(values = c("#FAEBB3", "#F7E28D", "#F6DB6E", "#F4D34E", "#F3CC2F",
"#f1c40f", "#EFB519", "#EDA722", "#EB982C", "#E98935",
"#E97A37", "#E86B39", "#E85B3A", "#e74c3c", "#D14A58",
"#BB4875", "#A44691", "#8e44ad", "#7859B9", "#616EC4",
"#4B83D0", "#3498db", "#33A5C1", "#31B2A6", "#30BF8C",
"#2ECC71", "#4ED487", "#6EDC9D", "#8DE3B2", "#8CE3B2"),
guide = guide_legend(override.aes = list(size = 6,
alpha = 1))) +
theme_minimal()
ggplot(data = na.omit(spp),
aes(x = y,
y = log10(publications),
colour = order)) +
geom_point(size = 3,
alpha = 0.4) +
labs(x = "Latitude (median)",
y = "Number of publications",
colour = "Order") +
scale_x_continuous(breaks = c(-40, 0, 40, 80),
labels = c("-40°", 0, "40°", "80°")) +
scale_y_continuous(breaks = c(0, 1, 2, 3, 4),
labels = c(1, 10, 100, "1,000", "10,000")) +
scale_colour_manual(values = c("#FAEBB3", "#F7E28D", "#F6DB6E", "#F4D34E", "#F3CC2F",
"#f1c40f", "#EFB519", "#EDA722", "#EB982C", "#E98935",
"#E97A37", "#E86B39", "#E85B3A", "#e74c3c", "#D14A58",
"#BB4875", "#A44691", "#8e44ad", "#7859B9", "#616EC4",
"#4B83D0", "#3498db", "#33A5C1", "#31B2A6", "#30BF8C",
"#2ECC71", "#4ED487", "#6EDC9D", "#8DE3B2", "#8CE3B2"),
guide = guide_legend(override.aes = list(size = 6,
alpha = 1))) +
bbc_style()
```
```{r}
#importing fonts
#if you are using extrafont
font_import() #press y + Enter to add fonts
fonttable()
#if you are using sysfont & showtext
font_add_google("Roboto Condensed")
showtext_auto()
```
```{r}
#creating our own theme
my_theme <- function() {
theme(axis.title = element_text(family = "Roboto Condensed",
size = 18,
face = "bold"),
#small tick marks
axis.text = element_text(family = "Roboto Condensed",
size = 12,
colour = "grey30"),
axis.line = element_line(size = 0.6,
colour = "grey60"),
legend.background = element_rect(fill = "white"),
legend.title = element_text(family = "Roboto Condensed",
size = 18,
face = "bold",
colour = "black"),
legend.text = element_text(family = "Roboto Condensed",
size = 16,
colour = "black"),
legend.key = element_rect(fill = "white"),
legend.position = "right",
legend.justification = "centre",
#area outside the panel
plot.title = element_blank(),
plot.background = element_rect(fill = "white"),
#panel is where you datapoints are
panel.background = element_rect(fill = "white"),
panel.grid.major = element_line(colour = "grey90"),
panel.grid.minor = element_blank())
}
plot <- ggplot(data = na.omit(spp),
aes(x = y,
y = log10(publications),
colour = order)) +
geom_point(size = 3,
alpha = 0.4) +
labs(x = "Latitude (median)",
y = "Number of publications",
colour = "Mammalian order") +
scale_x_continuous(breaks = c(-40, 0, 40, 80),
labels = c("-40°", 0, "40°", "80°")) +
scale_y_continuous(breaks = c(0, 1, 2, 3, 4),
labels = c(1, 10, 100, "1,000", "10,000")) +
scale_colour_manual(values = c("#FAEBB3", "#F7E28D", "#F6DB6E", "#F4D34E", "#F3CC2F",
"#f1c40f", "#EFB519", "#EDA722", "#EB982C", "#E98935",
"#E97A37", "#E86B39", "#E85B3A", "#e74c3c", "#D14A58",
"#BB4875", "#A44691", "#8e44ad", "#7859B9", "#616EC4",
"#4B83D0", "#3498db", "#33A5C1", "#31B2A6", "#30BF8C",
"#2ECC71", "#4ED487", "#6EDC9D", "#8DE3B2", "#8CE3B2"),
guide = guide_legend(override.aes = list(size = 6,
alpha = 1),
ncol = 1)) +
#add you own theme
my_theme()
ggsave("outputs/plot.pdf", plot, width = 16, height = 9, units = "in", dpi = 300)
```
```{r}
#dark mode!
my_theme_dark <- function() {
theme(axis.title = element_text(family = "Roboto Condensed",
size = 18,
face = "bold",
colour = "grey95"),
axis.text = element_text(family = "Roboto Condensed",
size = 12,
colour = "grey70"),
axis.line = element_line(size = 0.6,
colour = "grey40"),
legend.background = element_rect(fill = "black"),
legend.title = element_text(family = "Roboto Condensed",
size = 18,
face = "bold",
colour = "grey95"),
legend.text = element_text(family = "Roboto Condensed",
size = 16,
colour = "grey90"),
legend.key = element_rect(fill = "black"),
legend.position = "right",
legend.justification = "centre",
plot.title = element_blank(),
plot.background = element_rect(fill = "black"),
panel.background = element_rect(fill = "black"),
panel.grid.major = element_line(colour = "grey20"),
panel.grid.minor = element_blank())
}
plot_dark <- ggplot(data = na.omit(spp),
aes(x = y,
y = log10(publications),
colour = order)) +
geom_point(size = 3,
alpha = 0.4) +
labs(x = "Latitude (median)",
y = "Number of publications",
colour = "Mammalian order") +
scale_x_continuous(breaks = c(-40, 0, 40, 80),
labels = c("-40°", 0, "40°", "80°")) +
scale_y_continuous(breaks = c(0, 1, 2, 3, 4),
labels = c(1, 10, 100, "1,000", "10,000")) +
scale_colour_manual(values = c("#FAEBB3", "#F7E28D", "#F6DB6E", "#F4D34E", "#F3CC2F",
"#f1c40f", "#EFB519", "#EDA722", "#EB982C", "#E98935",
"#E97A37", "#E86B39", "#E85B3A", "#e74c3c", "#D14A58",
"#BB4875", "#A44691", "#8e44ad", "#7859B9", "#616EC4",
"#4B83D0", "#3498db", "#33A5C1", "#31B2A6", "#30BF8C",
"#2ECC71", "#4ED487", "#6EDC9D", "#8DE3B2", "#8CE3B2"),
guide = guide_legend(override.aes = list(size = 6,
alpha = 1),
ncol = 1)) +
my_theme_dark()
ggsave("outputs/plot_dark.pdf", plot_dark, width = 16, height = 9, units = "in", dpi = 300)
```