Skip to content

direction argument to scale_fill_fermenter does not behave as documented #4444

Closed

Description

@adviksh

Thank you for maintaining ggplot! I'm really enjoying using it in slides and Rmarkdown files as part of my reporting workflow.

I've been using the ColorBrewer palettes, and recently noticed that I'm not able to reverse their directions as expected. For example, thescale_fill_fermenter() function with a PuOr palette places purple on the low end and orange on the high end. I expected that setting direction = -1 argument would reverse the direction of the palette.

Instead, scale_fill_fermenter() behaves identically for direction = 1 and direction = -1. I can get my desired behavior by setting direction = "horizontal", but this isn't in the docs.

I did a quick search for this behavior in open issues, but the closest thing I could find (#4364) seemed different, so I'm opening the issue. It's not breaking my workflow, but I thought updating the docs or function might save others some time. If the fix is something a new contributor could tackle, I'm happy to give it a shot with a nudge in the right direction!

I've noticed this behavior in ggplot2 version 3.3.3, running in R version 4.0.2., and have a quick trio of examples below.

set.seed(2349)
library(ggplot2)

# Generate some data to plot
n = 100
tb = data.frame(x = runif(n),
                y = runif(n))

# Example 1
# Expected: discrete palette, with high values in orange and lows in purple.
# Actual: as expected.
ggplot(tb,
       aes(x = x, y = y, fill = y - x)) +
  scale_fill_fermenter(palette = 'PuOr') +
  geom_point(pch = 21, 
             color = 'white')

# Example 2
# Expected: discrete palette, with high values in *purple* and lows in *orange*
# Actual: same result as example 1. Highs are in *orange*.
ggplot(tb,
       aes(x = x, y = y, fill = y - x)) +
  scale_fill_fermenter(palette = 'PuOr',
                       direction = -1) +
  geom_point(pch = 21, 
             color = 'white')

# Example 3
# Expected: unknown! Found a stackoverflow answer that suggested this,
#           but it's not in the docs.
# Actual: discrete palette with purple on the high end, as desired.
ggplot(tb,
       aes(x = x, y = y, fill = y - x)) +
  scale_fill_fermenter(palette = 'PuOr',
                       direction = 'horizontal') +
  geom_point(pch = 21, 
             color = 'white')

Created on 2021-04-23 by the reprex package (v0.3.0)

Activity

  1. smouksassi commented on Apr 23, 2021

    @smouksassi

    the default is direction = - 1 then when you have direction = 1 it is reversed but then if you have anything other than - 1 you get the same results (scales::brewer_pal only tests for direction = -1)

    set.seed(2349)
    library(ggplot2)
    library(patchwork)
    # Generate some data to plot
    n = 100
    tb = data.frame(x = runif(n), y = runif(n))
    
    (ggplot(tb,  aes(x = x, y = y, fill = y - x)) +
      scale_fill_fermenter(palette = 'PuOr', direction = -1) +
      geom_point(pch = 21, color = 'white')+
      ggtitle("direction = -1")) +
    (ggplot(tb, aes(x = x, y = y, fill = y - x)) +
      scale_fill_fermenter(palette = 'PuOr', direction = 1) +
      geom_point(pch = 21, color = 'white')+
      ggtitle("direction = 1"))+
    (ggplot(tb, aes(x = x, y = y, fill = y - x)) +
      scale_fill_fermenter(palette = 'PuOr', direction = "blah") +
      geom_point(pch = 21,   color = 'white')+
      ggtitle("direction = blah"))

    #scales::brewer_pal only tests for direction = -1

    Created on 2021-04-23 by the reprex package (v2.0.0)

  2. adviksh commented on Apr 23, 2021

    @adviksh
    Author

    Ah I didn't realize the default direction switches from brewer to distiller/fermenter! This clears up my confusion and solves my problem, so I'm closing the issue — thank you!

  3. added a commit that references this issue on Aug 7, 2023
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions