Skip to content

Binned scales with manual color choices #4415

Closed

Description

@clauswilke

I tried the other day to use a binned scale where I would set the color gradient by manually picking colors, and I found myself unable to do so. (At least with only investing a moderate amount of time, reading the docs and trying a few different things.) Is there a way currently to do this? If so, could it be added to the docs? If not, could we add something like scale_fill_binned_manual() or scale_fill_binned_gradientn() for this purpose?

I also note that scale_fill_binned() is not documented at all right now. This is the man page that comes up for it: https://ggplot2.tidyverse.org/reference/scale_colour_continuous.html

For a reprex, how do I make the binned version of the following plot?

library(ggplot2)

ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
  geom_tile() +
  scale_fill_gradientn(
    colors = c("red", "green", "blue")
  )

Created on 2021-04-12 by the reprex package (v1.0.0)

@thomasp85 It would be great if some minimal version of this issue could be addressed for the 3.3.4 release, as binned scales with manual color choices are likely an important feature to anybody wanting to use binned scales in practice.

Activity

  1. teunbrand commented on Apr 12, 2021

    @teunbrand
    Collaborator

    There is scale_fill_stepsn() that parallels scale_fill_gradientn().

    library(ggplot2)
    
    ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
      geom_tile() +
      scale_fill_stepsn(
        colors = c("red", "green", "blue")
      )

    Created on 2021-04-12 by the reprex package (v1.0.0)

    I've seen some people on SO confused over this function though. They expected the first bin to be the first colour and the last bin to be the last colour. (see for example https://stackoverflow.com/q/65627153/11374827)

    I agree that scale_fill_binned_manual() would be convenient wherein the number of bins automatically matches the length of the values argument.

  2. clauswilke commented on Apr 12, 2021

    @clauswilke
    MemberAuthor

    I think scale_fill_stepsn() and related functions are perfectly reasonable and actually what I was looking for. So I think this issue is really about discoverability. Can we sprinkle some hints throughout the existing documentation that these functions exist? For example, refer from scale_fill_gradient() to scale_fill_steps() and vice versa. Also, add scale_fill_binned() to the pkgdown site and then from there refer to scale_fill_steps() etc.

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