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

Feature request: expose palette in discrete position scales #5770

Closed
teunbrand opened this issue Mar 12, 2024 · 2 comments · Fixed by #5771
Closed

Feature request: expose palette in discrete position scales #5770

teunbrand opened this issue Mar 12, 2024 · 2 comments · Fixed by #5771

Comments

@teunbrand
Copy link
Collaborator

The idea behind exposing a palette to discrete position scales is that the palette can be used to set custom spacings between discrete variables. Currently the 'palette' of discrete position scales is essentially seq_along(), and the ask here is to open this up to other functions that can translate discrete variables to continuous positions.

To give an example, let's say I have the following plot with a fake continuous scale separating groups of clarity:

library(ggplot2)

clarity_position <- function(x) {
  as.integer(x) + c(0, 1, 1, 2, 2, 3, 3, 4)[match(x, levels(x))] 
}
ggplot(diamonds, aes(clarity_position(clarity), price, group = clarity)) +
  geom_boxplot() +
  scale_x_continuous(
    breaks = c(1, 3, 4, 6, 7, 9, 10, 12),
    labels = levels(diamonds$clarity)
  )

Created on 2024-03-12 with reprex v2.1.0

I'd like to express a similar plot in the following way:

ggplot(diamonds, aes(clarity, price)) +
  geom_boxplot() +
  scale_x_discrete(
    palette = \(x) c(1, 3, 4, 6, 7, 9, 10, 12)
  )

I'd imagine it just uses the discrete expansion rules and not show minor breaks in the panel grid.

@davidhodge931
Copy link

davidhodge931 commented Apr 4, 2024

This looks useful :)

Wondering whether this argument should be called pal instead of palette, so that it aligns with {scales} terminology?

Or maybe the logic is a function that creates a palette uses pal_* and an argument within a function is called palette?

@teunbrand
Copy link
Collaborator Author

I don't think it is worth the hassle renaming the palette argument for this particular request as palette is a pre-existing argument to the scale constructors. But yeah the pal_*() functions from {scales} are function factories whose results are valid input for the palette arguments in ggplot2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants