Skip to content

Setting options(ggplot2.discrete.fill) overrides for aes(colour) with bad colourmap #4149

Closed

Description

@apoorvalal

I know the option to set colour palette is in its early stages, so wanted to flag an error I noticed.

Looks like setting ggplot2.discrete.fill overwrites the colourmap for colour (which should be unchanged based on the documentation?) with a cmap that is both ugly and unintelligible (and it hides the legend too for some reason).

``` r
library(ggplot2)
(p1 = ggplot(mpg, aes(class)) + geom_bar(aes(fill = drv)))

# unrelated plot 
(p2 = ggplot(mtcars, aes(x = wt,  y = mpg)) +
  geom_point(aes(colour = as.factor(cyl))))

# this should only apply to p1
options(ggplot2.discrete.fill    = function() scale_fill_viridis_d())
# as it does
p1

# but it also mysteriously overrides p2's colourmap 
p2

I'm running ggplot 3.3.2 in R 3.6.3 on a machine running ubuntu 18.04 LTS.

Activity

  1. yutannihilation commented on Jul 28, 2020

    @yutannihilation
    Member

    Thanks, confirmed. Overriding is for convenience and it works perfectly when a vector of colors is supplied (or specifying aesthetics = c("colour", "fill") in the scale function. But, it also leads us to unexpected behaviours like the one you encountered. here's another bad example. I agree this needs to be improved somehow.

    library(ggplot2)
    
    p <- ggplot(mpg, aes(class)) + geom_bar(aes(fill = drv))
    
    # Correct, and works as expected
    withr::with_options(
      list(ggplot2.discrete.fill = function() scale_fill_viridis_d()),
      print(p)
    )

    # Incorrect, and fails with a cryptic error
    withr::with_options(
      list(ggplot2.discrete.fill = function() scale_colour_viridis_d()),
      print(p)
    )
    #> Error: Unknown colour name: f
  2. added
    bug
    on Aug 31, 2020
  3. added this to the ggplot2 3.3.4 milestone on Mar 25, 2021
  4. thomasp85 commented on Apr 27, 2021

    @thomasp85
    Member

    @yutannihilation do you have any thoughts on how to best approach this?

  5. yutannihilation commented on Apr 27, 2021

    @yutannihilation
    Member

    I was thinking if it's possible to check aesthetics of the scale and tweak it (with some warning). For example:

    scale_colour_discrete <- function(..., type = getOption("ggplot2.discrete.colour", getOption("ggplot2.discrete.fill"))) {
      # TODO: eventually `type` should default to a set of colour-blind safe color codes (e.g. Okabe-Ito)
      type <- type %||% scale_colour_hue
      if (is.function(type)) {
        scale <- type(...)
        if (!"colour" %in% scale$aesthetics) {
          warn("...some nice warning...")
          scale$aesthetics <- c(scale$aesthetics, "colour")
        }
        scale
      } else {
        scale_colour_qualitative(..., type = type)
      }
    }
  6. thomasp85 commented on May 6, 2021

    @thomasp85
    Member

    @cpsievert are you relying on the colour->fill (or reverse) fallback in your theming packages? I'm very tempted to remove it to get it in line with how defaults are handled for continuous scales

  7. cpsievert commented on May 6, 2021

    @cpsievert
    Contributor

    @thomasp85 pretty sure I set both of them directly, so feel free to separate them

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugscales 🐍

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions