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

New labeller: label_dictionary() #461

Merged
merged 9 commits into from
Oct 22, 2024
Merged

New labeller: label_dictionary() #461

merged 9 commits into from
Oct 22, 2024

Conversation

teunbrand
Copy link
Contributor

This PR aims to fix #458.

Briefly, you pass a named vector and the breaks will be matched to the names, and the value will be returned as the label.

Examples rendered:

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

# Example lookup table
lut <- c(
  "4" = "four wheel drive",
  "r" = "rear wheel drive",
  "f" = "front wheel drive"
)

# Typical usage
demo_discrete(c("4", "r", "f"), labels = label_lut(lut))
#> scale_x_discrete(labels = label_lut(lut))

# By default, extra values ('w') will remain as-is
demo_discrete(c("4", "r", "f", "w"), labels = label_lut(lut))
#> scale_x_discrete(labels = label_lut(lut))

# Alternatively, you can relabel extra values
demo_discrete(
  c("4", "r", "f", "w"),
  labels = label_lut(lut, nomatch = "unknown")
)
#> scale_x_discrete(labels = label_lut(lut, nomatch = "unknown"))

Created on 2024-10-07 with reprex v2.1.1

@thomasp85
Copy link
Member

Still on the fence about the use of "lut". I don't think many users will immediately understand what it does

Don't know if label_dict() or label_dictionary() is more apt

@teunbrand
Copy link
Contributor Author

I'd be happy to change the name into either of these

@thomasp85
Copy link
Member

I feel we used the dict name somewhere else recently in scales or ggplot2?

@teunbrand
Copy link
Contributor Author

In tidyverse/ggplot2#6077, we'd allow labs(dictionary = ...) for a similar lookup.

@thomasp85
Copy link
Member

then let's mirror that

@teunbrand teunbrand changed the title New labeller: label_lut() New labeller: label_dictionary() Oct 22, 2024
@thomasp85
Copy link
Member

also need to rename the test file

@teunbrand
Copy link
Contributor Author

Good catch

@thomasp85 thomasp85 merged commit e5bb288 into r-lib:main Oct 22, 2024
14 checks passed
@teunbrand teunbrand deleted the label_lut branch October 22, 2024 08:17
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.

Feature request: lookup table based label function
2 participants