-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Description
I maintain an R package, afcharts, which provides users with custom continuous and discrete scales. To enable users to use these scales by default we have a function which modifies ggplot2 options to set the type argument of scale_fill_continuous etc. We're aware that this method is now superseded.
The continuous scale functions (scale_fill_continuous_af, scale_colour_continuous_af) have an na_colour argument, which sets the na.value argument when creating the scale. This argument has a default, which we would like to set to a value other than "grey50".
The following code used to work with version 3.5.2 of ggplot2, but with v4.0.1 gives an error relating to na.value. I think this is because na.value is being passed through both as an argument of scale_fill_continuous and scale_fill_continuous_af.
library(ggplot2)
library(afcharts)# afcharts has a function `use_afcharts` which sets ggplot2 scale options (equivalent to below)
options(
ggplot2.continuous.fill = scale_fill_continuous_af,
ggplot2.continuous.colour = scale_colour_continuous_af,
ggplot2.discrete.fill = scale_fill_discrete_af,
ggplot2.discrete.colour = scale_colour_discrete_af
)
# The user of the package uses the scales automatically
faithfuld2 <- faithfuld
faithfuld2$density[1:300] <- NA
ggplot(faithfuld2, aes(x = waiting, y = eruptions, fill = density)) +
geom_raster()Error in `geom_raster()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error in `ggplot2::continuous_scale()`:
! formal argument "na.value" matched by multiple actual arguments
If I wrap the custom scale functions within a function when setting the options it works again, although na.value is ignored if the user tries to use scale_fill_continuous directly.
options(
ggplot2.continuous.fill = function(){scale_fill_continuous_af()},
ggplot2.continuous.colour = function(){scale_colour_continuous_af()},
ggplot2.discrete.fill = function(){scale_fill_discrete_af()},
ggplot2.discrete.colour = function(){scale_colour_discrete_af()}
)
# Using scale_fill_continuous_af as default via options
ggplot(faithfuld2, aes(x = waiting, y = eruptions, fill = density)) +
geom_raster() +
scale_fill_continuous(na.value = "red")
# Compare to using scale_fill_coninuous_af directly where `na.value` can be set
ggplot(faithfuld2, aes(x = waiting, y = eruptions, fill = density)) +
geom_raster() +
scale_fill_continuous_af(na_colour = "red")
I think this may be related to issue #6710.
The above isn’t too big an issue, just raising in case there’s a quick fix.
I note that it is now possible to set default colour palettes using the theme which I may update afcharts to use
e.g. theme(palette.colour.continuous = scales::pal_viridis()). However, I don’t think we can use this for other aspects of the scale such as na.value and guide which we might want to set defaults for.