r-lib / scales

Tools for ggplot2 scales
https://scales.r-lib.org
Other
407 stars 109 forks source link

Feature request: `pal_size`, `pal_alpha`, & `pal_linewidth` #425

Closed davidhodge931 closed 7 months ago

davidhodge931 commented 7 months ago

Hi,

In the same way that pal_hue, pal_shape and pal_linewidth are useful helpers, could we also have pal_size, pal_alpha and pal_linewidth functions?

Maybe this would be good for tidy developer day?

Thanks!

teunbrand commented 7 months ago

I think these are all pal_rescale(). Colours, shapes and linetype have constraints on their output (e.g. must be valid col, pch and lty parameters). Size, alpha and linewidth all work with numerical values, so these have no such restraints and can all be fed through the pal_rescale() function. There is a pal_area() for the size though.

davidhodge931 commented 7 months ago

Thanks @teunbrand The use-case is mainly for something is reversed and you want to also reverse the other aesthetic to keep the order looking 'normal'. I can't work out how to use the pal_rescale() function to do this

library(tidyverse)
library(palmerpenguins)

p <- penguins |> 
  mutate(island = fct_rev(island)) |> 
  ggplot() +
  geom_point(aes(x = flipper_length_mm, 
                 y = island, 
                 size = island,
                 colour = island)) +
  scale_colour_manual(values = scales::pal_hue()(3)) +
  guides(colour = guide_legend(reverse = TRUE)) +
  guides(size = guide_legend(reverse = TRUE))

#I would like to be able to do this...
# p + 
#   scale_size_manual(values = scales::pal_size()(3))   

# I can't work out how to use scales::pal_rescale 
# p + 
#   scale_size_manual(values = scales::pal_rescale(range = c(1, 6))(3)) 
teunbrand commented 7 months ago

Right, so it seems this is more about the difference between discrete and continuous palettes then?

davidhodge931 commented 7 months ago

I suppose I want to easily be able to recreate the default values for when things are reversed

davidhodge931 commented 7 months ago

Might not work so well with the discrete/continuous thing

Maybe I'll just close.. feel free to reopen if you want to do something with this