tidyverse / ggplot2

An implementation of the Grammar of Graphics in R

Home Page:https://ggplot2.tidyverse.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bug where ggplot2.discrete.colour options set and then colour scale with limits applied

davidhodge931 opened this issue · comments

Not sure if I've misunderstood something, but I would expect this to work...

library(tidyverse)
library(palmerpenguins)

options(ggplot2.discrete.colour = function() ggplot2::scale_colour_viridis_d())

penguins |> 
  ggplot() +
  geom_point(
    aes(x = flipper_length_mm, 
        y = body_mass_g, 
        colour = species)
  ) +
  scale_colour_discrete(limits = c("Adelie", "Chinstrap"))
#> Error in (function () : unused argument (limits = c("Adelie", "Chinstrap"))

Created on 2024-07-08 with reprex v2.1.0

You need to forward the function arguments.

library(tidyverse)
library(palmerpenguins)

options(ggplot2.discrete.colour = function(...) ggplot2::scale_colour_viridis_d(...))

penguins |> 
  ggplot() +
  geom_point(
    aes(x = flipper_length_mm, 
        y = body_mass_g, 
        colour = species)
  ) +
  scale_colour_discrete(limits = c("Adelie", "Chinstrap"))
#> Warning: Removed 125 rows containing missing values or values outside the scale range
#> (`geom_point()`).

Created on 2024-07-07 by the reprex package (v2.0.1)

Indeed, or supply a scale function as-is: options(ggplot2.discrete.colour = ggplot2::scale_colour_viridis_d).