tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.39k stars 2k forks source link

binned_scale() function missing after version 3.4.1? #5942

Closed schmittrjp closed 3 weeks ago

schmittrjp commented 3 weeks ago

I have a lot of code that uses binned color scales similar to what was described in https://stackoverflow.com/questions/66318404/how-to-use-specific-filling-colors-when-using-scale-fill-binned/78614646#78614646

The reprex below is mostly taken from the thread on StackOverflow. Basically, the idea is to define exact colors for binned color scales.

This works well on ggplot v3.4.x.

Now I upgraded to 3.5.1 and I get an error

Error in list2(...) : object binned_scale not found

It seems like the function binned_scale is not a part of ggplot anymore. Has binned_scale been retired?

library(tidyverse)

dat <- mtcars %>% 
  group_by(cyl) %>% 
  summarise(n = n(),
            mean_hp = mean(hp)) %>% 
  ungroup

cols <- c("red", "blue", "grey50", "black")
ggplot(data = dat, aes(x = cyl, y = mean_hp, size = n, fill = n)) +
  geom_point(shape = 21) +
  scale_size_binned(breaks = c(8, 10, 12), guide = guide_bins(show.limits = T)) +
  labs(x = "Cylinder", y = "Mean hp", fill = "Nb of cars", size = "Nb of cars") +
  theme_minimal() +
  binned_scale(aesthetics = "fill", scale_name = "custom", 
               palette = ggplot2:::binned_pal(scales::manual_pal(values = cols)),
               guide = "bins",
               breaks = c(8, 10, 12), limits = c(min(dat$n), max(dat$n)), show.limits = T)

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

teunbrand commented 3 weeks ago

The binned_scale() function is not missing, it is exported as usual. The error message is about binned_pal, which is an internal function that was renamed to pal_binned(). This is the kind of risk one has to accept if you're using internal functions in your code.