-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathhelpers-gg.R
More file actions
182 lines (163 loc) · 5.13 KB
/
helpers-gg.R
File metadata and controls
182 lines (163 loc) · 5.13 KB
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
# ggplot2 convenience functions for internal use --------------------------
#' Do nothing plotting geom
#'
#' Adding `geom_ignore()` to a ggplot will not affect its appearance. This
#' function is useful for when plot elements should appear conditionally.
#'
#' @param ... arguments to a ggplot2 function to be ignored
#' @noRd
#' @examples
#' # Draw a vertical line at zero (or do nothing)
#' xs <- -2:2
#' maybe_vertical_line <- if (0 > min(xs) && 0 < max(xs)) {
#' vline_0(color = "gray90", linewidth = 0.5)
#' } else {
#' geom_ignore()
#' }
geom_ignore <- function(...) {
geom_blank(
mapping = NULL, data = NULL,
show.legend = FALSE, inherit.aes = FALSE)
}
#' Add new aesthetic mappings to a list of aesthetic mappings
#'
#' @param mapping a list of `uneval` aesthetic mappings (created by `aes()`)
#' @param ... additional mappings to add using `.data$` syntax
#' @return the updated list
#' @noRd
modify_aes <- function(mapping, ...) {
utils::modifyList(mapping, aes(...))
}
facet_wrap_parsed <- function(...) {
facet_wrap(..., labeller = label_parsed)
}
dont_expand_y_axis <- function(expand = c(0,0)) {
scale_y_continuous(expand = expand)
}
dont_expand_x_axis <- function(expand = c(0,0)) {
scale_x_continuous(expand = expand)
}
dont_expand_axes <- function() {
coord_cartesian(expand = FALSE)
}
force_axes_in_facets <- function() {
thm <- bayesplot_theme_get()
annotate(
"segment",
x = c(-Inf, -Inf), xend = c(Inf,-Inf),
y = c(-Inf,-Inf), yend = c(-Inf, Inf),
color = thm$axis.line$colour %||% thm$line$colour %||% "black",
linewidth = thm$axis.line$linewidth %||% thm$line$linewidth %||% 0.5
)
}
force_x_axis_in_facets <- function() {
thm <- bayesplot_theme_get()
annotate(
"segment",
x = -Inf, xend = Inf,
y = -Inf, yend = -Inf,
color = thm$axis.line$colour %||% thm$line$colour %||% "black",
linewidth = thm$axis.line$linewidth %||% thm$line$linewidth %||% 0.5
)
}
# Derive annotation line aesthetics from the active theme's gridlines.
#
# Because every current call-site draws a *vertical* reference line we first
# inspect the axis-specific element (`panel.grid.major.x`) so that themes
# which only customise vertical gridlines are handled correctly, then fall
# back to the general `panel.grid.major`.
#
# @param fallback_color,fallback_linewidth Values returned when the resolved
# grid element is blank or NULL (i.e. the theme hides gridlines). Different
# plots historically used different hardcoded values, so callers can preserve
# backward-compatible defaults.
annotation_style <- function(fallback_color = "gray90",
fallback_linewidth = 0.5) {
thm <- bayesplot_theme_get()
grid <- calc_element("panel.grid.major.x", thm)
if (inherits(grid, "element_blank") || is.null(grid)) {
grid <- calc_element("panel.grid.major", thm)
}
if (inherits(grid, "element_blank") || is.null(grid)) {
return(list(color = fallback_color, linewidth = fallback_linewidth))
}
minor <- calc_element("panel.grid.minor.x", thm)
if (inherits(minor, "element_blank") || is.null(minor)) {
minor <- calc_element("panel.grid.minor", thm)
}
minor_lw <- if (!inherits(minor, "element_blank") &&
!is.null(minor) &&
!is.null(minor$linewidth)) {
minor$linewidth
} else {
0.125
}
major_lw <- grid$linewidth %||% (minor_lw * 2)
list(
color = grid$colour %||% fallback_color,
linewidth = major_lw * 2
)
}
no_legend_spacing <- function() {
theme(legend.spacing.y = unit(0, "cm"))
}
reduce_legend_spacing <- function(cm) {
theme(legend.spacing.y = unit(-cm, "cm"))
}
space_legend_keys <- function(relative_size = 2, color = "white") {
theme(legend.key = element_rect(linewidth = rel(relative_size), color = color))
}
# set aesthetic mapping for histograms depending on freq argument
set_hist_aes <- function(freq = TRUE, ...) {
if (freq) {
# aes_(x = ~ value, ...)
aes(x = .data$value, ...)
} else {
# aes_(x = ~ value, y = ~ after_stat(density), ...)
aes(x = .data$value, y = after_stat(density), ...)
}
}
scale_color_ppc <-
function(name = NULL,
values = NULL,
labels = NULL,
...) {
scale_color_manual(
name = name %||% "",
values = values %||% get_color(c("dh", "lh")),
labels = labels %||% c(y_label(), yrep_label()),
...
)
}
scale_fill_ppc <-
function(name = NULL,
values = NULL,
labels = NULL,
...) {
scale_fill_manual(
name = name %||% "",
values = values %||% get_color(c("d", "l")),
labels = labels %||% c(y_label(), yrep_label()),
...
)
}
scale_color_ppd <-
function(name = NULL,
values = get_color("mh"),
labels = ypred_label(),
...) {
scale_color_ppc(name = name,
values = values,
labels = labels,
...)
}
scale_fill_ppd <-
function(name = NULL,
values = get_color("m"),
labels = ypred_label(),
...) {
scale_fill_ppc(name = name,
values = values,
labels = labels,
...)
}