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

Scale name function #6200

Merged
merged 7 commits into from
Jan 28, 2025
Merged

Scale name function #6200

merged 7 commits into from
Jan 28, 2025

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #4313.

Briefly, it enables users to set functions in labs(), scale_*() and guide_*() functions that are applied to the labels.

Here is an example:

devtools::load_all('~/packages/ggplot2/')
#> ℹ Loading ggplot2
df <- mtcars
attr(df$disp, "label") <- "engine displacment"

ggplot(df, aes(disp, mpg, colour = drat, shape = factor(cyl))) +
  geom_point() +
  labs(
    y = to_upper_ascii,
    shape = function(x) gsub("factor", "foo", x)
  ) +
  scale_shape_discrete(
    name = to_upper_ascii,
    guide = guide_legend(title = function(x) paste0(x, "!!!"))
  ) +
  scale_x_continuous(name = to_upper_ascii) +
  guides(colour = guide_colourbar(title = to_upper_ascii))

Created on 2024-11-25 with reprex v2.1.1

@@ -212,7 +212,7 @@ GuideBins <- ggproto(
key$.value <- 1 - key$.value
}

params$title <- scale$make_title(params$title %|W|% scale$name %|W|% title)
params$title <- scale$make_title(params$title, scale$name, title)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, this would happen in the base Guide class, but axes don't have access to labels.

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Merge branch 'main' into scale_name_function

# Conflicts:
#	R/labels.R
@teunbrand teunbrand merged commit 212224d into tidyverse:main Jan 28, 2025
13 checks passed
@teunbrand teunbrand deleted the scale_name_function branch January 28, 2025 08:16
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.

Allow scale name to be a function.
2 participants