Skip to content

Conversation

@teunbrand
Copy link
Collaborator

This PR aims to fix #6616.

Briefly, whenever a (default) label is derived from a NULL aesthetic, we set the fallback = TRUE attribute. This allows labels derived from other layers to overrule that label.

Reprex from the issue, notice that we have cty and hwy labels instead of x/y labels.

devtools::load_all("~/packages/ggplot2/")

ggplot(mpg) +
  aes(cty, hwy) +
  geom_rect(
    data = data.frame(xmin = 11, xmax = 35, ymin = 17, ymax = 44),
    map = aes(
      x = NULL, y = NULL,
      xmin = xmin, xmax = xmax, 
      ymin = ymin, ymax = ymax
    )
  ) +
  geom_point()

Created on 2025-09-24 with reprex v2.1.1

Comment on lines 353 to 358
if (is.null(mapping)) {
return(structure(aesthetic, fallback = TRUE))
}
if (is.atomic(mapping)) {
return(aesthetic)
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There is an argument to be made to also have atomic mappings return fallback labels.
#6616 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

yeah, I'm in favour of this

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I went ahead and implemented this after I pulsed your opinion in slack, so this should be ready for review

@teunbrand teunbrand added this to the ggplot2 4.0.1 milestone Oct 7, 2025
Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

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

LGTM

@teunbrand teunbrand merged commit 5f6d3c5 into tidyverse:main Oct 20, 2025
13 checks passed
@teunbrand teunbrand deleted the fix_NULL_aesthetics_label branch October 20, 2025 13:26
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.

setting axis aesthetics to x/y = NULL turns default axis labels into "x" / "y" instead of keeping the variable names of the next layer

2 participants