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

Palettes for discrete scales #5771

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

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #5770.

Briefly, it adds a palette argument to discrete scales that can be used to customise the position discrete variables are placed in.
Demonstrations perhaps show off the functionality better than text descriptions.

The reprex from the issue works:

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

p <- ggplot(diamonds, aes(clarity, price)) +
  geom_boxplot()

p + scale_x_discrete(palette = \(x) c(1, 3, 4, 6, 7, 9, 10, 12))

You can use the palette to invert the order:

p + scale_x_discrete(palette = \(x) -seq_along(x))

If two limits share a position, position adjustments treat these like a single position (invoking dodging in the example) but axis guides show duplicated labels.

p + scale_x_discrete(palette = \(x) c(1, 2, 2, 3, 3, 4, 4, 5))

Invalid palette output is checked:

p + scale_x_discrete(palette = \(x) x)
#> Error in `scale_x_discrete()`:
#> ! The `palette` function must return a <numeric> vector.

p + scale_x_discrete(palette = \(x) seq_along(x)[-1])
#> Error in `scale_x_discrete()`:
#> ! The `palette` function must return at least 8 values.

When mixing the discrete scale with continuous values in a different layer;

p2 <- p + annotate("line", x = c(1, 4.5, 8), y = c(5000, 10000, 5000))
p2

The continuous values stay as-is

p2 + scale_x_discrete(palette = \(x) c(1, 3, 4, 6, 7, 9, 10, 12))

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

There are at this moment two outstanding issues:

  • Discrete non-position scales take palette functions that take n as argument. In this PR, palette functions take the limits as arguments. Should I let palettes take n as an argument instead? I think the benefit of the current approach is that one could design a lookup table as palette argument.
  • I did not understand one test, see comment below.

@@ -116,6 +116,6 @@ test_that("expand_limits_scale_discrete() begrudgingly handles numeric limits",
coord_limits = c(NA, NA),
range_continuous = c(-15, -2)
),
c(-15, -2)
c(-16, -1)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I didn't understand this test, even looking through #3918 and #3919 where this was introduced.
Why should scale expansion take the narrower range here?
In any case, this PR essentially takes the range() of both ranges.

@teunbrand teunbrand marked this pull request as draft March 12, 2024 16:11
@teunbrand
Copy link
Collaborator Author

I've put the palettes in line with palletes elsewhere in that they now require an n argument instead of taking the limits verbatim.

@teunbrand teunbrand changed the title WIP: palettes for discrete scales Palettes for discrete scales Mar 22, 2024
@teunbrand teunbrand marked this pull request as ready for review March 22, 2024 08:54
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: expose palette in discrete position scales
1 participant