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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

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.

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.
1 participant