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

Default labels from attributes (option 1) #5878

Closed
wants to merge 6 commits into from

Conversation

teunbrand
Copy link
Collaborator

@teunbrand teunbrand commented May 2, 2024

This PR aims to fix #4631 and it competes with #5879.

Briefly, an attempt is made to derive default labels from the 'label' attribute.

Less brief, make_labels() now accepts a data argument. When there are simple symbolic mappings, these are evaluated and searched for a label attribute. There are some limitations with this approach that are detailed below.

To give an example we can give the mtcars dataset some label attributes.
Notice in the plot below that all default labels are derived from the attribute, regarless of whether it comes from the global mapping, the layer mapping or data pronoun use.

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

df <- mtcars
attr(df$mpg,  "label") <- "Miles per gallon"
attr(df$disp, "label") <- "Displacement"
attr(df$drat, "label") <- "Rear axle ratio"

ggplot(df, aes(mpg, .data$disp)) +
  geom_point(aes(colour = drat))

When we use a standalone + aes(...) it also yields the exact same plot.

ggplot(df) +
  aes(mpg, .data$disp) +
  geom_point(aes(colour = drat))

As for the limitations, here are some that I noticed.
When using layer data, global aesthetics do not get searched.

ggplot(mapping = aes(mpg, .data$disp)) +
  geom_point(aes(colour = drat), data = df)

Also, when the data is a function, the labels cannot be retrieved.
The data also isn't fortify()'ed or in any way preprocessed.

ggplot(df, aes(mpg, .data$disp)) +
  geom_point(aes(colour = drat), data = ~ subset(.x, mpg > 20))

Created on 2024-05-02 with reprex v2.1.0

The labels are also baked in at the time the layers are added, so doing the following:

p <- ggplot(mtcars, aes(mpg, .data$disp)) +
  geom_point(aes(colour = drat))
p %+% df

Does not give the attribute labels.

These limitations could be resolved if the labels were derived in the ggplot_build() step, but as that function is somewhat sacred, I tried this approach first. Also, as this does not have an on/off switch, this PR will break some plots (but hopefully for the better).

Despite these limitations, I think this is an improvement over the current situation. See #5879 for a second option with fewer limitations.

@olivroy
Copy link
Contributor

olivroy commented May 2, 2024

I really like this! With the newest support for the label attribute in RStudio, I am taking even more advantage of it. would be great if ggplot2 used it too! gt also does this by default

@teunbrand teunbrand changed the title Default labels from attributes Default labels from attributes (option 1) May 2, 2024
@teunbrand
Copy link
Collaborator Author

Closing this in favour of #5879

@teunbrand teunbrand closed this May 7, 2024
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.

Use label-attribute as default axis/legend title
2 participants