ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

Could `scale_*_manual` have a default `values` argument of `NULL`?

Open davidhodge931 opened this issue 5 months ago • 5 comments

The scale_*_manual functions are super useful. Is it possible that the values argument in these could default to NULL? This would allow users familiar with them to use them to change other non-values aspects of the scale through these functions

library(tidyverse)

p <- ggplot(mtcars, aes(mpg, wt)) +
  geom_point(aes(colour = factor(cyl)))

#you can do this currently
p + 
  scale_colour_manual(values = c("red", "blue", "green"),
                      breaks = c(4, 6))


#I would like to be able to do this
p + 
  scale_colour_manual(breaks = c(4, 6))
#> Error in `palette()`:
#> ! Insufficient values in manual scale. 3 needed but only 0 provided.

Created on 2024-01-20 with reprex v2.1.0

davidhodge931 avatar Jan 19 '24 21:01 davidhodge931

What would be the intended outcome of the manual scale without values?

teunbrand avatar Jan 20 '24 10:01 teunbrand

The outcome is that the manual scale turns up with NULL values, which would do whatever ggplot2 would do by default for the values if scale layer wasn't called

davidhodge931 avatar Jan 20 '24 20:01 davidhodge931

In the example below, I want the same graph to be produced without me having to specify what the values are

library(tidyverse)
library(palmerpenguins)

ggplot(mtcars, aes(mpg, wt)) +
  geom_point(aes(colour = factor(cyl))) + 
  scale_colour_manual(values = scales::pal_hue()(3), breaks = c(4, 6))

Created on 2024-01-21 with reprex v2.1.0

davidhodge931 avatar Jan 20 '24 20:01 davidhodge931

Right, I think this falls under the umbrella of #4269. To briefly recap, it has been on the wishlist for a while to set scale parameters without explicitly declaring the scale itself. This is similar to your example where you want to set the breaks but not other parameters, such as the palette.

There have been several attempts at implementing this, namely two in #4271 and one in #5444. The reason that these efforts have been fruitless, is because we cannot implement it in a clean way. Or maybe we can, but haven't found the way yet. I think it'd be only possible to do cleanly if the scale system gets a major overhaul.

That said, using scale_colour_discrete() sort of uses the default palette while leaving room to mess with breaks or limits etc.

teunbrand avatar Jan 20 '24 23:01 teunbrand

Thanks @teunbrand the partial scales thing looks super useful.

With regard to the scale_*_manual layers, can you not just make the values argument NULL, and then tell it to do the standard defaults when it is NULL?

Assume it's not that simple??

davidhodge931 avatar Jan 21 '24 03:01 davidhodge931