Skip to content

direction argument to scale_fill_fermenter does not behave as documented #4444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
adviksh opened this issue Apr 23, 2021 · 2 comments
Closed

Comments

@adviksh
Copy link

adviksh commented Apr 23, 2021

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)

@smouksassi
Copy link

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)

@adviksh
Copy link
Author

adviksh commented Apr 23, 2021

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!

@adviksh adviksh closed this as completed Apr 23, 2021
ahcyip added a commit to ahcyip/ggplot2 that referenced this issue Aug 7, 2023
Capturing in documentation that scale_*_distiller has a non-intuitive default direction = -1, and that to reverse, user needs to specify direction = 1

Source:
tidyverse#1439
tidyverse#4444
teunbrand pushed a commit that referenced this issue Sep 4, 2023
…_distiller's non-intuitive "direction" (#5381)

* Update scale-brewer.R

Capturing in documentation that scale_*_distiller has a non-intuitive default direction = -1, and that to reverse, user needs to specify direction = 1

Source:
#1439
#4444

* Move comment from Details to Note

* Document

---------

Co-authored-by: yutannihilation <yutannihilation@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants