-
Notifications
You must be signed in to change notification settings - Fork 227
/
pander.R
299 lines (276 loc) · 8.03 KB
/
pander.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
#' A ggplot theme originated from the pander package
#'
#' The \pkg{pander} ships with a default theme when the 'unify plots' option is
#' enabled via \code{panderOptions}, which is now also available outside of \pkg{pander} internals, like \code{evals},
#' \code{eval.msgs} or \code{Pandoc.brew}.
#' @inheritParams ggplot2::theme_bw
#' @param nomargin suppress the white space around the plot (boolean)
#' @param ff font family, like \code{sans}. Deprecated: use \code{base_family} instead.
#' @param fc font color (name or hexa code)
#' @param fs font size (integer). Deprecated: use \code{base_size} instead.
#' @param gM major grid (boolean)
#' @param gm minor grid (boolean)
#' @param gc grid color (name or hexa code)
#' @param gl grid line type (\code{lty})
#' @param boxes to render a border around the plot or not
#' @param bc background color (name or hexa code)
#' @param pc panel background color (name or hexa code)
#' @param lp legend position
#' @param axis axis angle as defined in \code{par(les)}
#' @export
#' @example inst/examples/ex-theme_pander.R
theme_pander <- function(base_size = 12, # nolint: cyclocomp_linter
base_family = "sans",
nomargin = TRUE,
ff = NULL,
fc = "black",
fs = NULL,
gM = TRUE, # nolint: object_name_linter
gm = TRUE,
gc = "grey",
gl = "dashed",
boxes = FALSE,
bc = "white",
pc = "transparent",
lp = "right",
axis = 1) {
if (hasArg(ff)) {
base_family <- ff
warning("Argument `ff` deprecated. Use `base_family` instead.")
}
if (hasArg(fs)) {
base_size <- fs
warning("Argument `fs` deprecated. Use `base_size` instead.")
}
if (requireNamespace("pander", quietly = TRUE)) {
if (missing(nomargin)) {
nomargin <- pander::panderOptions("graph.nomargin")
}
if (missing(base_family)) {
base_family <- pander::panderOptions("graph.fontfamily")
}
if (missing(fc)) {
fc <- pander::panderOptions("graph.fontcolor")
}
if (missing(base_size)) {
base_size <- pander::panderOptions("graph.fontsize")
}
if (missing(gM)) {
gM <- pander::panderOptions("graph.grid") # nolint: object_name_linter
}
if (missing(gm)) {
gm <- pander::panderOptions("graph.grid.minor")
}
if (missing(gc)) {
gc <- pander::panderOptions("graph.grid.color")
}
if (missing(gl)) {
gl <- pander::panderOptions("graph.grid.lty")
}
if (missing(boxes)) {
boxes <- pander::panderOptions("graph.boxes")
}
if (missing(bc)) {
bc <- pander::panderOptions("graph.background")
}
if (missing(pc)) {
pc <- pander::panderOptions("graph.panel.background")
}
if (missing(lp)) {
lp <- pander::panderOptions("graph.legend.position")
}
if (missing(axis)) {
axis <- pander::panderOptions("graph.axis.angle")
}
}
## DRY
tc <- ifelse(pc == "transparent", bc, pc) # 'transparent' color
## default colors, font and legend position
res <- theme(
text = element_text(family = base_family),
plot.background = element_rect(fill = bc, colour = NA),
panel.grid = element_line(
colour = gc,
linewidth = 0.2, linetype = gl
),
panel.grid.minor = element_line(linewidth = 0.1),
axis.ticks = element_line(
colour = gc,
linewidth = 0.2
),
plot.title = element_text(
colour = fc,
face = "bold",
size = base_size * 1.2
),
axis.text = element_text(
colour = fc,
face = "plain", size = base_size * 0.8
),
legend.text = element_text(
colour = fc, face = "plain",
size = base_size * 0.8
),
legend.title = element_text(
colour = fc,
face = "italic",
size = base_size
),
axis.title.x = element_text(
colour = fc,
face = "plain",
size = base_size
),
strip.text.x = element_text(
colour = fc,
face = "plain",
size = base_size
),
axis.title.y = element_text(
colour = fc,
face = "plain",
size = base_size,
angle = 90
),
strip.text.y = element_text(
colour = fc,
face = "plain",
size = base_size,
angle = -90
),
legend.key = element_rect(colour = gc, fill = "transparent"),
strip.background = element_rect(
colour = gc,
fill = "transparent"
),
panel.border = element_rect(fill = NA, colour = gc),
panel.background = element_rect(fill = pc, colour = gc),
legend.position = lp
)
## disable box(es) around the plot
if (!isTRUE(boxes)) {
res <- res + theme(
legend.key = element_rect(
colour = "transparent",
fill = "transparent"
),
strip.background = element_rect(
colour = "transparent",
fill = "transparent"
),
panel.border = element_rect(
fill = NA,
colour = tc
),
panel.background = element_rect(
fill = pc,
colour = tc
)
)
}
## disable grid
if (!isTRUE(gM)) {
res <- res + theme(
panel.grid = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
)
}
## disable minor grid
if (!isTRUE(gm)) {
res <- res + theme(panel.grid.minor = element_blank())
}
## margin
if (nomargin) {
res <- res + theme(plot.margin = unit(c(0.1, 0.1, 0.1, 0), "lines"))
}
## axis angle (TODO: DRY with ifelse in the default color etc. section)
if (axis == 0) {
res <- res + theme(axis.text.y = element_text(
colour = fc,
family = base_family,
face = "plain",
size = base_size * 0.8,
angle = 90
))
}
if (axis == 2) {
res <- res + theme(axis.text.x = element_text(
colour = fc,
family = base_family,
face = "plain",
size = base_size * 0.8,
angle = 90,
hjust = 1
))
}
if (axis == 3) {
res <- res + theme(
axis.text.y = element_text(
colour = fc,
family = base_family,
face = "plain",
size = base_size * 0.8,
angle = 90
),
axis.text.x = element_text(
colour = fc,
family = base_family,
face = "plain",
size = base_size * 0.8,
angle = 90,
hjust = 1
)
)
}
res
}
#' Color palette from the pander package
#'
#' The \pkg{pander} ships with a default colorblind and printer-friendly
#' color palette borrowed from \verb{https://jfly.iam.u-tokyo.ac.jp/color/}.
#'
#' @param n number of colors. This palette supports up to eight colors.
#' @param random_order if the palette should be reordered randomly before
#' rendering each plot to get colorful images
#' @export
#' @family colour pander
#' @example inst/examples/ex-palette_pander.R
palette_pander <- function(n, random_order = FALSE) {
## default (colorblind and printer-friendly) colors
cols <- c(
"#56B4E9", "#009E73", "#F0E442", "#0072B2",
"#D55E00", "#CC79A7", "#999999", "#E69F00"
)
if (requireNamespace("pander", quietly = TRUE)) {
cols <- pander::panderOptions("graph.colors")
}
if (isTRUE(random_order)) {
cols <- sample(cols)
}
if (length(cols) < n) {
cols <- rep(cols, length.out = n)
}
cols[1:n]
}
#' Color scale from the pander package
#'
#' The \pkg{pander} ships with a default colorblind and printer-friendly color
#' palette borrowed from \verb{https://jfly.iam.u-tokyo.ac.jp/color/}.
#' @inheritParams ggplot2::scale_colour_hue
#' @inheritParams palette_pander
#' @family colour pander
#' @rdname scale_pander
#' @seealso \code{\link{theme_pander}()}
#' @export
scale_color_pander <- function(...) {
discrete_scale("colour", "pander", palette_pander, ...)
}
#' @rdname scale_pander
#' @export
scale_colour_pander <- scale_color_pander
#' @rdname scale_pander
#' @export
scale_fill_pander <- function(...) {
discrete_scale("fill", "pander", palette_pander, ...)
}