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

Deprioritise annotations in deriving labels #6316

Open
teunbrand opened this issue Jan 31, 2025 · 5 comments · May be fixed by #6322
Open

Deprioritise annotations in deriving labels #6316

teunbrand opened this issue Jan 31, 2025 · 5 comments · May be fixed by #6322

Comments

@teunbrand
Copy link
Collaborator

This was first suggested in tidyverts/feasts#166 (comment).

When you use an annotation you want to use as the background, you use that annotation before the real 'data' layers. For example, to highlight the 70s you can use:

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2

ggplot(economics, aes(date, unemploy)) +
  annotate(
    "polygon",
    x = as.Date(c("1970-01-01", "1970-01-01", "1980-01-01", "1980-01-01")),
    y = c(Inf, -Inf, -Inf, Inf),
    alpha = 0.25
  ) +
  geom_line()

Created on 2025-01-31 with reprex v2.1.1

However the default label is now x and not date and the question is whether we should deprioritise annotations in deriving automatic labels? Currently, there is nothing distinguishing an annotation layer from a regular layer beyond the constructor.

@clauswilke
Copy link
Member

I think the general rule would be to use global aesthetics before layer-specific aesthetics. Not sure how easy that would be to implement. This might require a complete rethinking of when and how axis titles are defined.

@teunbrand
Copy link
Collaborator Author

We explicitly avoided that approach based on #5894.
It does not mesh well with automatically extracting the label attribute, as the global mapping is never evaluated and hence the attribute cannot be retrieved.

@clauswilke
Copy link
Member

I see. I guess there's always going to be edge cases that are difficult to catch. In the end, people can always override labels manually if they need to.

@teunbrand
Copy link
Collaborator Author

True enough. Just to repeat a bit of reasoning from the original discussion, an argument why annotations should be skipped is that the annotation's aesthetics is not (directly) 'mapped data' unlike regular layers. In addition, it will always give 'x' as the default label for the x aesthetic because that is how the column names are constructed internally, so it will never be informative for global data.

@teunbrand
Copy link
Collaborator Author

Arguably we also shouldn't let expand_limits() affect labels. In the example below, I think displ is the more appropriate x-axis title.

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2

ggplot(mpg, aes(displ, hwy)) +
  expand_limits(x = c(0, 10)) +
  geom_point()

Created on 2025-02-05 with reprex v2.1.1

@teunbrand teunbrand linked a pull request Feb 5, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants