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

Could support the use of function factories like scales::pal_hue() and scales::pal_viridis()? #5843

Open
davidhodge931 opened this issue Apr 15, 2024 · 1 comment
Labels
feature a feature request or enhancement scales 🐍

Comments

@davidhodge931
Copy link

davidhodge931 commented Apr 15, 2024

It'd be great if it was possible to do this for all of the non-positional scales, especially colour/fill

library(tidyverse)
library(palmerpenguins)

penguins |>
  ggplot() +
  geom_point(aes(x = flipper_length_mm,
                 y = body_mass_g,
                 col = species)) +
  scale_colour_manual(values = scales::pal_hue())

penguins |>
  ggplot() +
  geom_point(aes(x = flipper_length_mm,
                 y = body_mass_g,
                 col = bill_depth_mm)) +
  scale_colour_gradientn(colours = scales::pal_viridis())

penguins |>
  ggplot() +
  geom_point(aes(x = flipper_length_mm,
                 y = body_mass_g,
                 col = bill_depth_mm)) +
  scale_colour_stepsn(colours = scales::pal_viridis())
@davidhodge931 davidhodge931 changed the title Could colour/fill scales support the use of function factories like scales::pal_hue() and scales::pal_viridis()? Could support the use of function factories like scales::pal_hue() and scales::pal_viridis()? Apr 15, 2024
@teunbrand
Copy link
Collaborator

teunbrand commented Apr 15, 2024

This has been on my mind as well to make the interface between scales and palettes somewhat smoother.
Your first example can be replaced with discrete_scale("colour", palette = scales::pal_hue()), but this is not so much the case for the continuous examples.

For this to happen, I think {scales} needs to start describing palette functions more so that some properties are easily queried.
Useful properties to know about palettes are:

  • Is it continuous, i.e. does it accept values in the (0, 1) range.
  • Is it discrete, i.e. does it accept n values.
  • If discrete, what is the maximum n it will accept?
  • Do they have built-in NA handlers (e.g. via scales::colour_ramp())?

If we know this, we can automatically expand discrete palettes to continuous ones by using e.g. colour_ramp(discrete_pal()(n_max)), or discretise continuous palettes by using continuous_pal()(seq(0, 1, length.out = n).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement scales 🐍
Projects
None yet
Development

No branches or pull requests

2 participants