Skip to content

Palettes for discrete scales - #5771

Merged
teunbrand merged 10 commits into
tidyverse:mainfrom
teunbrand:discrete_palette
May 20, 2024
Merged

teunbrand merged 10 commits into
tidyverse:mainfrom
teunbrand:discrete_palette

Conversation

@teunbrand

Copy link
Copy Markdown
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.

range_continuous = c(-15, -2)
),
c(-15, -2)
c(-16, -1)

Copy link
Copy Markdown
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
Copy Markdown
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

@thomasp85 thomasp85 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. I dislike the palette name for this functionality, but I guess this is in line with the terminology we have ended up with

@teunbrand

Copy link
Copy Markdown
Collaborator Author

I dislike the palette name for this functionality

Yeah I also don't think it is a great name, but it is consistent with the rest of the scales api

teunbrand added 3 commits May 20, 2024 13:04
Merge branch 'main' into discrete_palette

# Conflicts:
#	R/scale-discrete-.R
@teunbrand
teunbrand merged commit 7a006da into tidyverse:main May 20, 2024
@teunbrand
teunbrand deleted the discrete_palette branch May 20, 2024 11:42
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

2 participants