forked from hadley/ggplot2-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.R
115 lines (97 loc) · 2.71 KB
/
common.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
library(ggplot2)
library(dplyr)
library(tidyr)
options(digits = 3, dplyr.print_min = 6, dplyr.print_max = 6)
knitr::opts_chunk$set(
comment = "#>",
collapse = TRUE,
fig.path = paste0("_figures/", chapter, "/"),
fig.show = "hold",
dpi = 300,
cache = TRUE,
cache.path = paste0("_cache/", chapter, "/")
)
is_latex <- function() {
identical(knitr::opts_knit$get("rmarkdown.pandoc.to"), "latex")
}
columns <- function(n, aspect_ratio = 1, max_width = if (n == 1) 0.65 else 1) {
if (is_latex()) {
out_width <- paste0(round(max_width / n, 3), "\\linewidth")
knitr::knit_hooks$set(plot = plot_hook_bookdown)
} else {
out_width <- paste0(round(max_width * 100 / n, 1), "%")
}
width <- 6 / n * max_width
knitr::opts_chunk$set(
fig.width = width,
fig.height = width * aspect_ratio,
fig.align = if (max_width < 1) "center" else "default",
fig.show = if (n == 1) "asis" else "hold",
fig.retina = NULL,
out.width = out_width,
out.extra = if (!is_latex())
paste0("style='max-width: ", round(width, 2), "in'")
)
}
# Draw parts of plots -----------------------------------------------------
draw_legends <- function(...) {
plots <- list(...)
gtables <- lapply(plots, function(x) ggplot_gtable(ggplot_build(x)))
guides <- lapply(gtables, gtable::gtable_filter, "guide-box")
one <- Reduce(function(x, y) cbind(x, y, size = "first"), guides)
grid::grid.newpage()
grid::grid.draw(one)
}
# Customised plot layout --------------------------------------------------
plot_hook_bookdown <- function(x, options) {
paste0(
begin_figure(x, options),
include_graphics(x, options),
end_figure(x, options)
)
}
begin_figure <- function(x, options) {
if (!knitr_first_plot(options))
return("")
paste0(
"\\begin{figure}[H]\n",
if (options$fig.align == "center") " \\centering\n"
)
}
end_figure <- function(x, options) {
if (!knitr_last_plot(options))
return("")
paste0(
if (!is.null(options$fig.cap)) {
paste0(
' \\caption{', options$fig.cap, '}\n',
' \\label{fig:', options$label, '}\n'
)
},
"\\end{figure}\n"
)
}
include_graphics <- function(x, options) {
opts <- c(
sprintf('width=%s', options$out.width),
sprintf('height=%s', options$out.height),
options$out.extra
)
if (length(opts) > 0) {
opts_str <- paste0("[", paste(opts, collapse = ", "), "]")
} else {
opts_str <- ""
}
paste0(" \\includegraphics",
opts_str,
"{", knitr:::sans_ext(x), "}",
if (options$fig.cur != options$fig.num) "%",
"\n"
)
}
knitr_first_plot <- function(x) {
x$fig.show != "hold" || x$fig.cur == 1L
}
knitr_last_plot <- function(x) {
x$fig.show != "hold" || x$fig.cur == x$fig.num
}