Skip to content

Commit 998b4b9

Browse files
authored
Merge pull request #292 from stan-dev/aes_-deprecation
Handle deprecation of `aes_` (ggplot2) and `.data` (tidyselect)
2 parents d562b37 + e2a7908 commit 998b4b9

File tree

100 files changed

+2372
-2109
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+2372
-2109
lines changed

.github/workflows/R-CMD-check.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
push:
55
branches: [main, master]
66
pull_request:
7-
branches: [main, master]
7+
branches: [main, master, patch-for-cran]
88

99
name: R-CMD-check
1010

.github/workflows/test-coverage.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
push:
55
branches: [main, master]
66
pull_request:
7-
branches: [main, master]
7+
branches: [main, master, patch-for-cran]
88

99
name: test-coverage
1010

R/bayesplot-ggplot-themes.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ theme_default <-
4545
panel.grid = element_blank(),
4646
panel.background = element_blank(),
4747
panel.border = element_blank(),
48-
axis.line = element_line(size = 0.4),
49-
axis.ticks = element_line(size = 0.3),
48+
axis.line = element_line(linewidth = 0.4),
49+
axis.ticks = element_line(linewidth = 0.3),
5050
strip.background = element_blank(),
5151
strip.text = element_text(size = rel(0.9)),
5252
strip.placement = "outside",

R/bayesplot-helpers.R

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@
114114
#'
115115
#' # vertical line at zero (with some optional styling)
116116
#' p + vline_0()
117-
#' p + vline_0(size = 0.25, color = "darkgray", linetype = 2)
117+
#' p + vline_0(linewidth = 0.25, color = "darkgray", linetype = 2)
118118
#'
119119
#' # vertical line(s) at specified values
120120
#' v <- c(-0.5, 0, 0.5)
121-
#' p + vline_at(v, linetype = 3, size = 0.25)
121+
#' p + vline_at(v, linetype = 3, linewidth = 0.25)
122122
#'
123-
#' my_lines <- vline_at(v, alpha = 0.25, size = 0.75 * c(1, 2, 1),
123+
#' my_lines <- vline_at(v, alpha = 0.25, linewidth = 0.75 * c(1, 2, 1),
124124
#' color = c("maroon", "skyblue", "violet"))
125125
#' p + my_lines
126126
#'
@@ -131,10 +131,10 @@
131131
#' p <- mcmc_intervals(x, regex_pars = "beta")
132132
#' p + vline_at(x[, 3:4], colMeans)
133133
#' p + vline_at(x[, 3:4], "colMeans", color = "darkgray",
134-
#' lty = 2, size = 0.25)
134+
#' lty = 2, linewidth = 0.25)
135135
#' p + vline_at(x[, 3:4], function(a) apply(a, 2, mean),
136136
#' color = "orange",
137-
#' size = 2, alpha = 0.1)
137+
#' linewidth = 2, alpha = 0.1)
138138
#' }
139139
#'
140140
#' # using the lbub function to get interval lower and upper bounds (lb, ub)
@@ -146,9 +146,9 @@
146146
#'
147147
#' b1 <- x[, "beta[1]"]
148148
#' p2 + vline_at(b1, fun = lbub(0.8), color = "gray20",
149-
#' size = 2 * c(1,.5,1), alpha = 0.75)
149+
#' linewidth = 2 * c(1,.5,1), alpha = 0.75)
150150
#' p2 + vline_at(b1, lbub(0.8, med = FALSE), color = "gray20",
151-
#' size = 2, alpha = 0.75)
151+
#' linewidth = 2, alpha = 0.75)
152152
#'
153153
#'
154154
#' ##########################
@@ -185,7 +185,7 @@
185185
#' myfacets +
186186
#' yaxis_text(FALSE) +
187187
#' yaxis_ticks(FALSE) +
188-
#' xaxis_ticks(size = 1, color = "skyblue")
188+
#' xaxis_ticks(linewidth = 1, color = "skyblue")
189189
#' }
190190
#'
191191
#' ##############################
@@ -209,7 +209,7 @@
209209
#' legend_text(size = 14) +
210210
#' legend_move(c(0.75, 0.5)) +
211211
#' plot_bg(fill = "gray90") +
212-
#' panel_bg(color = "black", fill = "gray99", size = 3)
212+
#' panel_bg(color = "black", fill = "gray99", linewidth = 3)
213213
#' }
214214
#'
215215
#'
@@ -225,12 +225,14 @@
225225
#' fun = dnorm,
226226
#' args = list(mean(x[,, "beta[1]"]), sd(x[,, "beta[1]"])),
227227
#' color = "purple",
228-
#' size = 2
228+
#' linewidth = 2
229229
#' )
230230
#'
231231
#' color_scheme_set("gray")
232-
#' mcmc_hist(x, pars = "beta[1]") + purple_gaussian
233-
#' \donttest{mcmc_dens(x, pars = "beta[1]") + purple_gaussian}
232+
#' mcmc_hist(x, pars = "beta[1]", freq = FALSE) + purple_gaussian
233+
#' \donttest{
234+
#' mcmc_dens(x, pars = "beta[1]") + purple_gaussian
235+
#' }
234236
#'
235237
NULL
236238

@@ -301,14 +303,16 @@ lbub <- function(p, med = TRUE) {
301303

302304
# internal
303305
calc_v <- function(v, fun, fun_args, ...) {
304-
if (missing(v))
306+
if (missing(v)) {
305307
abort("'v' can't be missing.")
306-
if (missing(fun))
308+
}
309+
if (missing(fun)) {
307310
return(v)
311+
}
308312
f <- match.fun(fun)
309-
if (missing(fun_args))
313+
if (missing(fun_args)) {
310314
return(f(v))
311-
315+
}
312316
do.call(f, c(list(v), fun_args))
313317
}
314318

@@ -347,8 +351,9 @@ legend_text <- function(...) {
347351
#' @rdname bayesplot-helpers
348352
#' @export
349353
xaxis_title <- function(on = TRUE, ...) {
350-
if (!on)
354+
if (!on) {
351355
return(xlab(NULL))
356+
}
352357
theme(axis.title.x = element_text(...))
353358
}
354359
#' @rdname bayesplot-helpers
@@ -441,15 +446,15 @@ plot_bg <- function(on = TRUE, ...) {
441446
#'
442447
grid_lines <- function(color = "gray50", size = 0.2) {
443448
theme(
444-
panel.grid.major = element_line(color = color, size = size),
445-
panel.grid.minor = element_line(color = color, size = size * 0.5)
449+
panel.grid.major = element_line(color = color, linewidth = size),
450+
panel.grid.minor = element_line(color = color, linewidth = size * 0.5)
446451
)
447452
}
448453

449454
grid_lines_y <- function(color = "gray50", size = 0.2) {
450455
theme(
451-
panel.grid.major.y = element_line(color = color, size = size),
452-
panel.grid.minor.y = element_line(color = color, size = size * 0.5)
456+
panel.grid.major.y = element_line(color = color, linewidth = size),
457+
panel.grid.minor.y = element_line(color = color, linewidth = size * 0.5)
453458
)
454459
}
455460

R/helpers-gg.R

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#' # Draw a vertical line at zero (or do nothing)
1212
#' xs <- -2:2
1313
#' maybe_vertical_line <- if (0 > min(xs) && 0 < max(xs)) {
14-
#' vline_0(color = "gray90", size = 0.5)
14+
#' vline_0(color = "gray90", linewidth = 0.5)
1515
#' } else {
1616
#' geom_ignore()
1717
#' }
@@ -48,6 +48,13 @@ modify_aes_ <- function(mapping, ...) {
4848
utils::modifyList(mapping, aes_(...))
4949
}
5050

51+
#' Same as `modify_aes_` but using `aes()` instead of `aes_()` (now deprecated).
52+
#' Often `...` will need to contain expression of the form `.data$x` to avoid R cmd check warnings
53+
#' @noRd
54+
modify_aes <- function(mapping, ...) {
55+
utils::modifyList(mapping, aes(...))
56+
}
57+
5158

5259

5360
facet_wrap_parsed <- function(...) {
@@ -69,7 +76,7 @@ force_axes_in_facets <- function() {
6976
x = c(-Inf, -Inf), xend = c(Inf,-Inf),
7077
y = c(-Inf,-Inf), yend = c(-Inf, Inf),
7178
color = thm$axis.line$colour %||% thm$line$colour %||% "black",
72-
size = thm$axis.line$size %||% thm$line$size %||% 0.5
79+
linewidth = thm$axis.line$linewidth %||% thm$line$linewidth %||% 0.5
7380
)
7481
}
7582

@@ -80,7 +87,7 @@ force_x_axis_in_facets <- function() {
8087
x = -Inf, xend = Inf,
8188
y = -Inf, yend = -Inf,
8289
color = thm$axis.line$colour %||% thm$line$colour %||% "black",
83-
size = thm$axis.line$size %||% thm$line$size %||% 0.5
90+
linewidth = thm$axis.line$linewidth %||% thm$line$linewidth %||% 0.5
8491
)
8592
}
8693

@@ -91,16 +98,18 @@ reduce_legend_spacing <- function(cm) {
9198
theme(legend.spacing.y = unit(-cm, "cm"))
9299
}
93100
space_legend_keys <- function(relative_size = 2, color = "white") {
94-
theme(legend.key = element_rect(size = rel(relative_size), color = color))
101+
theme(legend.key = element_rect(linewidth = rel(relative_size), color = color))
95102
}
96103

97104

98105
# set aesthetic mapping for histograms depending on freq argument
99106
set_hist_aes <- function(freq = TRUE, ...) {
100107
if (freq) {
101-
aes_(x = ~ value, ...)
108+
# aes_(x = ~ value, ...)
109+
aes(x = .data$value, ...)
102110
} else {
103-
aes_(x = ~ value, y = ~ after_stat(density), ...)
111+
# aes_(x = ~ value, y = ~ after_stat(density), ...)
112+
aes(x = .data$value, y = after_stat(density), ...)
104113
}
105114
}
106115

R/mcmc-diagnostics-nuts.R

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ mcmc_nuts_acceptance <-
154154
Mean = mean(.data$Value),
155155
Median = median(.data$Value))
156156

157-
hists <- ggplot(data, aes_(x = ~ Value, y = ~ after_stat(density))) +
157+
hists <- ggplot(data, aes(x = .data$Value, y = after_stat(density))) +
158158
geom_histogram(
159159
fill = get_color("l"),
160160
color = get_color("lh"),
161-
size = .25,
161+
linewidth = 0.25,
162162
na.rm = TRUE,
163163
binwidth = binwidth
164164
) +
@@ -167,12 +167,12 @@ mcmc_nuts_acceptance <-
167167
if (!overlay_chain) {
168168
hists <- hists +
169169
geom_vline(
170-
aes_(xintercept = ~ Mean),
170+
aes(xintercept = .data$Mean),
171171
data = stats_par,
172172
color = get_color("dh")
173173
) +
174174
geom_vline(
175-
aes_(xintercept = ~ Median),
175+
aes(xintercept = .data$Median),
176176
data = stats_par,
177177
color = get_color("d"),
178178
linetype = 2
@@ -186,9 +186,13 @@ mcmc_nuts_acceptance <-
186186
yaxis_ticks(FALSE) +
187187
xaxis_title(FALSE)
188188

189-
scatter <- ggplot(NULL) +
189+
scatter_data <- data.frame(
190+
x = accept_stat$Value,
191+
y = lp$Value
192+
)
193+
scatter <- ggplot(scatter_data) +
190194
geom_point(
191-
aes_(x = ~ accept_stat$Value, y = ~ lp$Value),
195+
aes(x = .data$x, y = .data$y),
192196
alpha = 0.75,
193197
shape = 21,
194198
fill = get_color(ifelse(overlay_chain, "l", "m")),
@@ -208,12 +212,16 @@ mcmc_nuts_acceptance <-
208212
binwidth = binwidth
209213
)
210214

215+
chain_scatter_data <- data.frame(
216+
x = accept_stat$Value[accept_stat$Chain == chain],
217+
y = lp$Value[lp$Chain == chain]
218+
)
211219
scatter <- scatter +
212220
geom_point(
213-
aes_(x = ~ accept_stat$Value[accept_stat$Chain == chain],
214-
y = ~ lp$Value[lp$Chain == chain]),
221+
aes(x = .data$x, y = .data$y),
215222
color = get_color("d"),
216-
alpha = 0.5
223+
alpha = 0.5,
224+
data = chain_scatter_data
217225
)
218226
}
219227

@@ -250,19 +258,19 @@ mcmc_nuts_divergence <- function(x, lp, chain = NULL, ...) {
250258
labels = c("No divergence", "Divergence"))
251259

252260
violin_lp_data <- data.frame(divergent, lp = lp$Value)
253-
violin_lp <- ggplot(violin_lp_data, aes_(x = ~ Value, y = ~ lp)) +
261+
violin_lp <- ggplot(violin_lp_data, aes(x = .data$Value, y = .data$lp)) +
254262
geom_violin(fill = get_color("l"), color = get_color("lh")) +
255263
ylab("lp__") +
256264
xaxis_title(FALSE) +
257-
bayesplot_theme_get()
265+
bayesplot_theme_get()
258266

259267
violin_accept_stat_data <- data.frame(divergent, as = accept_stat$Value)
260-
violin_accept_stat <- ggplot(violin_accept_stat_data, aes_(x = ~ Value, y = ~ as)) +
268+
violin_accept_stat <- ggplot(violin_accept_stat_data, aes(x = .data$Value, y = .data$as)) +
261269
geom_violin(fill = get_color("l"), color = get_color("lh")) +
262270
ylab("accept_stat__") +
263271
scale_y_continuous(limits = c(NA, 1.05)) +
264272
xaxis_title(FALSE) +
265-
bayesplot_theme_get()
273+
bayesplot_theme_get()
266274

267275
div_count <- table(divergent$Value)[[2]]
268276
div_text <- ngettext(div_count, "divergence", "divergences")
@@ -312,7 +320,7 @@ mcmc_nuts_stepsize <- function(x, lp, chain = NULL, ...) {
312320
stepsize_labels <- scale_x_discrete(labels = stepsize_labels_text)
313321

314322
violin_lp_data <- dplyr::left_join(lp, stepsize_by_chain, by = "Chain")
315-
violin_lp <- ggplot(violin_lp_data, aes_(x = ~as.factor(ss), y = ~Value)) +
323+
violin_lp <- ggplot(violin_lp_data, aes(x = as.factor(.data$ss), y = .data$Value)) +
316324
geom_violin(fill = get_color("l"), color = get_color("lh")) +
317325
ylab("lp__") +
318326
stepsize_labels +
@@ -322,7 +330,7 @@ mcmc_nuts_stepsize <- function(x, lp, chain = NULL, ...) {
322330
violin_accept_stat_data <-
323331
dplyr::left_join(accept_stat, stepsize_by_chain, by = "Chain")
324332
violin_accept_stat <-
325-
ggplot(violin_accept_stat_data, aes_(x = ~as.factor(ss), y = ~Value)) +
333+
ggplot(violin_accept_stat_data, aes(x = as.factor(.data$ss), y = .data$Value)) +
326334
geom_violin(fill = get_color("l"), color = get_color("lh")) +
327335
ylab("accept_stat__") +
328336
scale_y_continuous(limits = c(NA, 1.05)) +
@@ -354,11 +362,11 @@ mcmc_nuts_treedepth <- function(x, lp, chain = NULL, ...) {
354362
treedepth <- dplyr::filter(x, .data$Parameter == "treedepth__")
355363
accept_stat <- dplyr::filter(x, .data$Parameter == "accept_stat__")
356364

357-
hist_td <- ggplot(treedepth, aes_(x = ~ Value, y = ~ after_stat(density))) +
365+
hist_td <- ggplot(treedepth, aes(x = .data$Value, y = after_stat(density))) +
358366
geom_histogram(
359367
fill = get_color("l"),
360368
color = get_color("lh"),
361-
size = .2,
369+
linewidth = 0.2,
362370
na.rm = TRUE,
363371
binwidth = 1
364372
) +
@@ -370,14 +378,14 @@ mcmc_nuts_treedepth <- function(x, lp, chain = NULL, ...) {
370378

371379
violin_lp_data <- data.frame(treedepth, lp = lp$Value)
372380
violin_lp <-
373-
ggplot(violin_lp_data, aes_(x = ~ factor(Value), y = ~ lp)) +
381+
ggplot(violin_lp_data, aes(x = factor(.data$Value), y = .data$lp)) +
374382
geom_violin(fill = get_color("l"), color = get_color("lh")) +
375383
labs(x = "treedepth__", y = "lp__") +
376384
bayesplot_theme_get()
377385

378386
violin_accept_stat_data <- data.frame(treedepth, as = accept_stat$Value)
379387
violin_accept_stat <-
380-
ggplot(violin_accept_stat_data, aes_(x = ~ factor(Value), y = ~ as)) +
388+
ggplot(violin_accept_stat_data, aes(x = factor(.data$Value), y = .data$as)) +
381389
geom_violin(fill = get_color("l"), color = get_color("lh")) +
382390
labs(x = "treedepth__", y = "accept_stat__") +
383391
scale_y_continuous(breaks = c(0, 0.5, 1)) +
@@ -444,28 +452,29 @@ mcmc_nuts_energy <-
444452
mutate(
445453
Ediff = .data$Value - dplyr::lag(.data$Value),
446454
E_centered = .data$Value - mean(.data$Value),
447-
Ediff_centered = .data$Ediff - mean(.data$Ediff, na.rm = TRUE))
455+
Ediff_centered = .data$Ediff - mean(.data$Ediff, na.rm = TRUE)
456+
)
448457

449458
fills <- set_names(get_color(c("l", "m")), c("E_fill", "Ediff_fill"))
450459
clrs <- set_names(get_color(c("lh", "mh")), c("E_fill", "Ediff_fill"))
451460
aes_labs <- c(expression(pi[E]), expression(pi[paste(Delta, E)]))
452461

453-
graph <- ggplot(data, aes_(y = ~ after_stat(density))) +
462+
graph <- ggplot(data, aes(y = after_stat(density))) +
454463
geom_histogram(
455-
aes_(
456-
x = ~ Ediff_centered,
457-
fill = ~ "Ediff_fill",
458-
color = ~ "Ediff_fill"
464+
aes(
465+
x = .data$Ediff_centered,
466+
fill = "Ediff_fill",
467+
color = "Ediff_fill"
459468
),
460469
linewidth = 0.25,
461470
na.rm = TRUE,
462471
binwidth = binwidth
463472
) +
464473
geom_histogram(
465-
aes_(
466-
x = ~ E_centered,
467-
fill = ~ "E_fill",
468-
color = ~ "E_fill"
474+
aes(
475+
x = .data$E_centered,
476+
fill = "E_fill",
477+
color = "E_fill"
469478
),
470479
linewidth = 0.25,
471480
na.rm = TRUE,

0 commit comments

Comments
 (0)