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

Label dictionaries #5178

Open
teunbrand opened this issue Feb 5, 2023 · 2 comments · May be fixed by #6077
Open

Label dictionaries #5178

teunbrand opened this issue Feb 5, 2023 · 2 comments · May be fixed by #6077
Labels
feature a feature request or enhancement

Comments

@teunbrand
Copy link
Collaborator

I recently saw this tweet, which I thought was a nice suggestion, because it allows you to reuse labels without falling victims to typos and such. It still required a lot of manual management though.

Then, I realised we have several related issues/feature requests already, for example #4631 or #4313. However, I think having a label dictionary is perhaps a more elegant approach than hardcoding a path to retrieve labels from attributes (see also #4631 (comment)).

For the vanilla case, I think it should work something like this:

library(ggplot2)

label_dictionary <- c(
  displ = "Engine displacement [L]",
  hwy   = "Highway miles per gallon",
  "factor(cyl)" = "Number of cylinders" # Maybe I should be able to omit the function call?
)

# What I would like to specify
ggplot(mpg, aes(displ, hwy, colour = factor(cyl))) +
  geom_point() +
  labs(dict = label_dictionary)

# What I would want it to look like
ggplot(mpg, aes(displ, hwy, colour = factor(cyl))) +
  geom_point() +
  labs(!!!setNames(label_dictionary, c("x", "y", "colour")))

Created on 2023-02-05 with reprex v2.0.2

However, for the {Hmisc/tinylabels/labelled/sjlabelled} cases, we might be able to specify dict = derive() (or similar) to specifically instruct to lookup label names in the data.

@teunbrand teunbrand added the feature a feature request or enhancement label Jul 18, 2023
@teunbrand teunbrand linked a pull request Sep 4, 2024 that will close this issue
@aphalo
Copy link
Contributor

aphalo commented Sep 9, 2024

@teunbrand This feature will be very useful! It will allow me to not only to simplify the code in 'ggspectra' but, more importantly, easy my plans to implement support for multiple languages.

@teunbrand
Copy link
Collaborator Author

I'm glad you can see the potential! If you want to give it a spin before it hits the main branch, you can use pak::pak("tidyverse/ggplot2#6077") to install that functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants