Skip to content

Duplicated aes warning with multiple modified aesthetics #4707

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

Merged
merged 10 commits into from
Apr 29, 2022

Conversation

teunbrand
Copy link
Collaborator

This is a simple fix for some warnings that are thrown when multiple modified aesthetics are used.
The warnings could be triggered with the reprex below. With this PR, these warnings won't be triggered.

# Reprexed before PR
library(ggplot2)
library(colorspace)

df <- data.frame(x = 1, y = 1, lab = "test")

ggplot(df, aes(x, y, label = lab)) +
  geom_label(
    aes(colour = stage(lab, after_scale = darken(colour, 0.3)),
        fill   = after_scale(lighten(colour, 0.5)))
  )
#> Warning: Duplicated aesthetics after name standardisation: NA
#> Warning: Duplicated aesthetics after name standardisation: NA

Effectively, the two chunks of code below take the names of the (modified) aesthetics twice.

names(modified_aes) <- rename_aes(names(modifiers))

ggplot2/R/aes.r

Lines 168 to 169 in 759c63c

rename_aes <- function(x) {
names(x) <- standardise_aes_names(names(x))

This surprisingly works well when there is only a single modified aesthetic, but not so much with multiple ones.

The following erroneous code (duplicated colour/color):

ggplot(df, aes(x, y, label = lab)) +
  geom_label(
    aes(colour = stage(lab, after_scale = darken(colour, 0.3)),
        color  = after_scale(lighten(color, 0.5)))
  )

Used to throw these warnings:

#> Warning: Duplicated aesthetics after name standardisation: colour
#> Warning: Duplicated aesthetics after name standardisation: NA
#> Warning: Duplicated aesthetics after name standardisation: NA

And now throws the following warnings:

#> Warning: Duplicated aesthetics after name standardisation: colour
#> Warning: Duplicated aesthetics after name standardisation: colour
#> Warning: Duplicated aesthetics after name standardisation: colour

@hadley
Copy link
Member

hadley commented Mar 15, 2022

Could you please add a simple unit test that uses expect_snapshot_warning() to capture the warning messages?

@teunbrand
Copy link
Collaborator Author

Sure, are you OK with bumping the {testthat} requirement to >=3.1.2 for this test?

@hadley
Copy link
Member

hadley commented Mar 15, 2022

Yes, that's fine.

@teunbrand
Copy link
Collaborator Author

Thanks, I also bumped rlang to >=1.0.0 due to a recently added use of on_load().

test_that("staged aesthetics warn appropriately for duplicated names", {
# Test should *not* report `NA` as the duplicated aes (#4707)
df <- data.frame(x = 1, y = 1, lab = "test")
expr <- substitute(
Copy link
Member

Choose a reason for hiding this comment

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

Since I don't think you're actually doing any substitution, quote() would be more appropriate here. But I think you could also just do p <- ggplot() + ... in the expect_snapshot_warning().

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good points, I wasn't aware of this, so thanks!

)
# One warning in plot code due to evaluation of `aes()`
expect_snapshot_warning(p <- eval(expr))
# Two warnings in building due to `stage()`/`after_scale()`
Copy link
Member

Choose a reason for hiding this comment

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

I only see one warning in the test?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah indeed! The second of those warnings only occurred when ggplot_build(p) was evaluated in the console, due to automatically printing the result. I've now disabled the legend, which caused that extra warning.

Copy link
Member

@hadley hadley left a comment

Choose a reason for hiding this comment

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

Thanks! The last thing is to add a bullet to the top of NEWS.md. It should briefly describe the change and end with (@yourname, #issuenumber).

@hadley hadley merged commit a6c22e7 into tidyverse:main Apr 29, 2022
@hadley
Copy link
Member

hadley commented Apr 29, 2022

Thanks!

@teunbrand teunbrand deleted the duplicated_aes_warning branch May 14, 2022 17:05
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.

2 participants