Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change deprecated ggplot2 syntax to current recommendations #1026

Merged
merged 5 commits into from
Sep 10, 2023

Conversation

bwiernik
Copy link
Contributor

@bwiernik bwiernik commented Oct 3, 2022

Summary:

ggplot2 recently added a linewidth argument to replace size for line geoms and elements. Currently, loading rstan or a package that loads it produces a deprecation warning about the size argument. This PR replaces size with linewidth for line geoms and elements in the rstan plotting functions. The PR also updates a few other instances of deprecated ggplot2 syntax with the recommended alternatives (replacing aes_string() with aes(.data$...), replacing "..density.." with after_stat(density)).

Intended Effect:

No visual changes to plot output. Removal of deprecation warnings on package load.

How to Verify:

Output is unchanged for the following plots with the current develop version and this PR:

library(brms)
library(rstan)
m <- brm(mpg ~ disp, data = mtcars)
stan_dens(m$fit)
stan_hist(m$fit)
stan_plot(m$fit)

Documentation:

https://github.com/tidyverse/ggplot2/blob/main/NEWS.md

Copyright and Licensing

Please list the copyright holder for the work you are submitting (this will be you or your assignee, such as a university or company):

Brenton M. Wiernik

By submitting this pull request, the copyright holder is agreeing to license the submitted work under the following licenses:

base <-
ggplot2::ggplot(
plot_data$samp,
ggplot2::aes_string(x = "iteration", y = "value", color = "chain")
ggplot2::aes(x = .data$iteration, y = .data$value, color = .data$chain)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's no need for .data$ with aes(). This applies everywhere.

Suggested change
ggplot2::aes(x = .data$iteration, y = .data$value, color = .data$chain)
ggplot2::aes(x = iteration, y = value, color = chain)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the .data$ is necessary to avoid global variable name NOTEs in CRAN checks

@hsbadr
Copy link
Member

hsbadr commented Mar 16, 2023

@bwiernik Can you address the comment above and rebase this on the experimental branch?

graph <- base + ggplot2::geom_violin(color = .NUTS_CLR, fill = .NUTS_FILL)
if (chain == 0) return(graph)
chain_clr <- color_vector_chain(ncol(df_x))[chain]
chain_fill <- chain_clr
chain_data <- data.frame(x = as.factor(df_x[, chain]), y = df_y[, chain])
graph + ggplot2::geom_violin(
data = chain_data,
mapping = ggplot2::aes_string("x", "y"),
mapping = ggplot2::ggplot2::aes(x = .data$x, y = .data$y),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mapping = ggplot2::ggplot2::aes(x = .data$x, y = .data$y),
mapping = ggplot2::aes(x = .data$x, y = .data$y),

Copy link
Contributor

@bgoodri bgoodri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am guessing this is OK, although I batched a few suggestions.

@bwiernik
Copy link
Contributor Author

bwiernik commented Sep 8, 2023

I removed another aes_q() that had been added on the experimental branch. The .data pronoun inside aes() is necessary because aes(x = value) will trigger a Undeclared Global Variable Name NOTE in R CMD CHECK. data$value or data[['value']] is the current recommended syntax for situations like this.

From ?aes_string

All these functions are soft-deprecated. Please use tidy evaluation idioms instead. Regarding aes_string(), you can replace it with .data pronoun. For example, the following code can achieve the same mapping as aes_string(x_var, y_var).

x_var <- "foo"
y_var <- "bar"
aes(.data[[x_var]], .data[[y_var]])

See vignette("ggplot2-in-packages").

@jgabry
Copy link
Member

jgabry commented Sep 8, 2023

Thanks @bwiernik!

@bgoodri
Copy link
Contributor

bgoodri commented Sep 9, 2023

Is this good to merge now?

@jgabry
Copy link
Member

jgabry commented Sep 9, 2023

Were the linewidth changes merged already at some point? I don’t see them in the Files Changed view, but they are in the commit history. The Files Changed view now just shows aes and .data stuff, which looks good to go.

@bwiernik
Copy link
Contributor Author

It looks like all of the commits but the most recent are showing as already merged.

@hsbadr
Copy link
Member

hsbadr commented Sep 10, 2023

Yes, I tested this in the experimental.

@hsbadr hsbadr self-requested a review September 10, 2023 19:33
@hsbadr hsbadr merged commit 4722cae into stan-dev:develop Sep 10, 2023
7 checks passed
@bwiernik bwiernik deleted the linewidth branch September 13, 2023 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants