forked from rstudio/pagedown
-
Notifications
You must be signed in to change notification settings - Fork 8
/
unhcr_ggplot_theme.R
317 lines (278 loc) · 13.2 KB
/
unhcr_ggplot_theme.R
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
#' @name theme_foundation
#' @rdname theme_foundation
#' @title Base clean ggplot theme to build from
#' @export theme_foundation
#' @author ipda | unhcr.org | 2020
theme_foundation <- function(base_size=12, base_family="") {
thm <- theme_grey(base_size = base_size, base_family = base_family)
for (i in names(thm)) {
if ("colour" %in% names(thm[[i]])) {
thm[[i]]["colour"] <- list(NULL)
}
if ("fill" %in% names(thm[[i]])) {
thm[[i]]["fill"] <- list(NULL)
}
}
thm + theme(panel.border = element_rect(fill = NA),
legend.background = element_rect(colour = NA),
line = element_line(colour = "black"),
rect = element_rect(fill = "white", colour = "black"),
text = element_text(colour = "black"))
}
#' @name unhcr_theme
#' @rdname unhcr_theme
#' @title Basic UNHCR theme for ggplot2
#' @export unhcr_theme
#' @author ipda | unhcr.org | 2020
#'
#' @description \code{unhcr_theme} provides a basic \bold{UNHCR} theme
#' to use in \bold{ggplot2} commands.
#'
#' @param base_family Base font family (optional, character).
#' Default: \code{base_family = "Lato"}.
#' Options include \code{"mono"}, \code{"sans"} (default), and "serif".
#' @param base_size Base font size (optional, numeric).
#' Default: \code{base_size = 11}.
#' Recommendations use 11 for print and 16 for web
#' @param plot_title_family,plot_title_face,plot_title_size,plot_title_margin plot title family, face, size and margin
#' @param subtitle_family,subtitle_face,subtitle_size plot subtitle family, face and size
#' @param subtitle_margin plot subtitle margin bottom (single numeric value)
#' @param strip_text_family,strip_text_face,strip_text_size facet label font family, face and size
#' @param caption_family,caption_face,caption_size,caption_margin plot caption family, face, size and margin
#' @param axis_title_family,axis_title_face,axis_title_size axis title font family, face and size
#' @param axis_title_just axis title font justification, one of `[blmcrt]`
#' @param legend_title_family,legend_title_face,legend_title_size axis title font family, face and size
#' @param legend_title legend title if `TRUE` add legend title; default `FALSE`
#' @param plot_margin plot margin (specify with `ggplot2::margin()`)
#' @param grid_col,axis_col grid & axis colors
#' @param grid panel grid (`TRUE`, `FALSE`, or a combination of `X`, `x`, `Y`, `y`)
#' @param axis_text_size font size of axis text
#' @param axis add x or y axes? `TRUE`, `FALSE`, "`xy`"
#' @param ticks ticks if `TRUE` add ticks
#'
#' @examples {
#'
#' if (!require(ggplot2)) {
#' install.packages('ggplot2') # installs ggplot2
#' library('ggplot2') # loads ggplot2
#' }
#' # Check if ggplot2 is loaded:
#' if ("ggplot2" %in% (.packages())){
#' message("Package 'ggplot2' is loaded.")
#' } # if ("ggplot2" %in% (.packages())) end.
#' library(ggplot2)
#' library(tidyverse)
#' update_geom_font_defaults()
#'
#' # Bar chart
#' count(mpg, class) %>%
#' ggplot(aes(class, n)) +
#' geom_col() +
#' geom_text(aes(label=n), nudge_y=3) +
#' labs(x="Fuel efficiency (mpg)", y="Weight (tons)",
#' title="Bar chart example",
#' subtitle="A plot that is only useful for demonstration purposes",
#' caption="Brought to you by ...") +
#' unhcr_theme(grid="Y") +
#' theme(axis.text.y=element_blank())
#' }
#'
#' @family plot functions
#'
#' @import ggplot2
unhcr_theme <- function(base_family="Lato",
base_size = 12,
plot_title_family=base_family,
plot_title_size = base_size*1.5,
plot_title_face="bold",
plot_title_margin = base_size*0.75,
subtitle_family=base_family,
subtitle_size = base_size*1.25,
subtitle_face = "plain",
subtitle_margin = base_size,
strip_text_family = base_family,
strip_text_size = base_size,
strip_text_face = "plain",
caption_family = base_family,
caption_size = base_size*0.75,
caption_face = "plain",
caption_margin = base_size*0.75,
axis_text_size = base_size,
axis_title_family = subtitle_family,
axis_title_size = base_size*0.85,
axis_title_face = "plain",
axis_title_just = "rt",
legend_title_family = base_family,
legend_title_size = base_size*0.85,
legend_title_face = "plain",
legend_title = FALSE,
plot_margin = margin(15, 15, 15, 15),
grid_col = grey(.80, 1),
grid = TRUE,
axis_col = grey(.40, 1),
axis = FALSE,
ticks = FALSE) {
ret <- theme_foundation(base_family=base_family, base_size=base_size)
## legend ----
ret <- ret + theme(legend.background=element_blank())
ret <- ret + theme(legend.key=element_blank())
ret <- ret + theme(legend.position = "top")
ret <- ret + theme(legend.justification = 'right')
ret <- ret + theme(legend.direction = "horizontal")
if (inherits(legend_title, "character") | legend_title == TRUE) {
ret <- ret + theme(legend.title=element_text(size=legend_title_size, family=legend_title_family,
face = legend_title_face))
} else {
ret <- ret + theme(legend.title = element_blank())
}
## Line ----
ret <- ret + theme(line = element_line(colour = grey(.80, 1)))
ret <- ret + theme(rect = element_rect(fill = "transparent", linetype = 0, colour = NA))
ret <- ret + theme(text = element_text(colour = grey(.15, 1)))
## grid ----
if (inherits(grid, "character") | grid == TRUE) {
ret <- ret + theme(panel.grid=element_line(color=grid_col, size=0.5))
ret <- ret + theme(panel.grid.major=element_line(color=grid_col, size=0.5))
ret <- ret + theme(panel.grid.minor=element_line(color=grid_col, size=0.25))
if (inherits(grid, "character")) {
if (regexpr("X", grid)[1] < 0) ret <- ret + theme(panel.grid.major.x=element_blank())
if (regexpr("Y", grid)[1] < 0) ret <- ret + theme(panel.grid.major.y=element_blank())
if (regexpr("x", grid)[1] < 0) ret <- ret + theme(panel.grid.minor.x=element_blank())
if (regexpr("y", grid)[1] < 0) ret <- ret + theme(panel.grid.minor.y=element_blank())
}
} else {
ret <- ret + theme(panel.grid=element_blank())
}
ret <- ret + theme(panel.spacing=grid::unit(2, "lines"))
## axis ----
if (inherits(axis, "character") | axis == TRUE) {
ret <- ret + theme(axis.line=element_line(color=grey(.40, 1), size=0.2))
if (inherits(axis, "character")) {
axis <- tolower(axis)
if (regexpr("x", axis)[1] < 0) {
ret <- ret + theme(axis.line.x=element_blank())
} else {
ret <- ret + theme(axis.line.x=element_line(color=axis_col, size=0.2))
}
if (regexpr("y", axis)[1] < 0) {
ret <- ret + theme(axis.line.y=element_blank())
} else {
ret <- ret + theme(axis.line.y=element_line(color=axis_col, size=0.2))
}
} else {
ret <- ret + theme(axis.line.x=element_line(color=axis_col, size=0.2))
ret <- ret + theme(axis.line.y=element_line(color=axis_col, size=0.2))
}
} else {
ret <- ret + theme(axis.line=element_blank())
}
if (!ticks) {
ret <- ret + theme(axis.ticks = element_blank())
ret <- ret + theme(axis.ticks.x = element_blank())
ret <- ret + theme(axis.ticks.y = element_blank())
} else {
ret <- ret + theme(axis.ticks = element_line(size=0.15))
ret <- ret + theme(axis.ticks.x = element_line(size=0.15))
ret <- ret + theme(axis.ticks.y = element_line(size=0.15))
ret <- ret + theme(axis.ticks.length = grid::unit(5, "pt"))
}
xj <- switch(tolower(substr(axis_title_just, 1, 1)), b=0, l=0, m=0.5, c=0.5, r=1, t=1)
yj <- switch(tolower(substr(axis_title_just, 2, 2)), b=0, l=0, m=0.5, c=0.5, r=1, t=1)
ret <- ret + theme(axis.text.x=element_text(size=axis_text_size,
margin=margin(t=0)))
ret <- ret + theme(axis.text.y=element_text(size=axis_text_size,
margin=margin(r=0)))
ret <- ret + theme(axis.title=element_text(size=axis_title_size,
family=axis_title_family,
color = grey(.30, 1)))
ret <- ret + theme(axis.title.x=element_text(hjust=xj,
size=axis_title_size,
family=axis_title_family,
face=axis_title_face))
ret <- ret + theme(axis.title.y=element_text(hjust=yj,
size=axis_title_size,
family=axis_title_family,
face=axis_title_face))
ret <- ret + theme(axis.title.y.right=element_text(hjust=yj,
size=axis_title_size,
angle=90,
family=axis_title_family,
face=axis_title_face))
## text ----
ret <- ret + theme(plot.title=element_text(hjust=0,
size=plot_title_size,
margin=margin(b=plot_title_margin),
family=plot_title_family,
face=plot_title_face))
ret <- ret + theme(plot.subtitle=element_text(hjust=0,
size=subtitle_size,
margin=margin(b=subtitle_margin),
family=subtitle_family,
face=subtitle_face))
ret <- ret + theme(plot.caption=element_text(hjust=0, size=caption_size,
margin=margin(t=caption_margin),
family=caption_family,
face=caption_face,
color = grey(.60, 1)))
ret <- ret + theme(strip.text=element_text(hjust=0,
size=strip_text_size,
face=strip_text_face,
family=strip_text_family))
## position ----
ret <- ret + theme(plot.title.position = "plot")
ret <- ret + theme(plot.caption.position = "plot")
ret <- ret + theme(plot.margin=plot_margin)
ret
}
#' @name update_geom_font_defaults
#' @rdname update_geom_font_defaults
#' @title Update matching font defaults for text geoms
#' @export update_geom_font_defaults
#' @author ipda | unhcr.org | 2020
#' @description Updates [ggplot2::geom_label] and [ggplot2::geom_text] font defaults
#' @param family font family name
#' @param face font face
#' @param size font size
#' @param color font color
update_geom_font_defaults <- function(family="Lato", face="plain", size=3.5,
color = grey(.15, 1)) {
update_geom_defaults("text", list(family=family, face=face, size=size, color=color))
update_geom_defaults("label", list(family=family, face=face, size=size, color=color))
}
#' @name unhcr_discrete
#' @rdname unhcr_discrete
#' @title Ggplot scale color discrete and continuous palette based on the unhcr_pal_graphic
#' @export unhcr_discrete
#' @author ipda | unhcr.org | 2020
#' @examples {
#' library(scales)
#' ## to fix: the below...
#' #scales::show_col(unhcr_discrete(9))
#' }
unhcr_discrete <- function() { manual_pal(as.vector(t(unhcr_pal_graphic))) }
#' @name unhcr_scale_colour_discrete
#' @rdname unhcr_scale_colour_discrete
#' @title Discrete color scales based on the UNHCR graphic palette
#' @export unhcr_scale_colour_discrete
#' @author ipda | unhcr.org | 2020
#' @description See [unhcr_discrete()].
#' @aliases unhcr_scale_color_discrete
#' @inheritDotParams ggplot2::discrete_scale -expand -position
unhcr_scale_colour_discrete <- function(...) { discrete_scale("colour",
"unhcr_pal_graphic",
unhcr_discrete(), ...) }
#' @name unhcr_scale_fill_discrete
#' @rdname unhcr_scale_fill_discrete
#' @title Discrete fill scales based on the UNHCR graphic palette
#' @export unhcr_scale_fill_discrete
#' @author ipda | unhcr.org | 2020
#' @description See [unhcr_discrete()].
#' @aliases unhcr_scale_colour_discrete
#' @export unhcr_scale_fill_discrete
unhcr_scale_fill_discrete <- function(...) { discrete_scale("fill",
"unhcr_pal_graphic",
unhcr_discrete(), ...) }
## ToDo: ------
# - axis_title TRUE/FALSE?
# - Fix example
# fix unhcr_discrete