tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.48k stars 2.02k forks source link

Default labels from attributes (option 1) #5878

Closed teunbrand closed 5 months ago

teunbrand commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

Closing this in favour of #5879