Skip to content

scale_fill_steps produces unevenly spaced legend when limits are set #4019

Description

@Shians

When setting the limits of scale_fill_steps(), the fill brackets in the legend becomes unevenly spaced. It's not clear why or how this happens.

library(tidyverse)

df <- tibble(
    crossing(
        tibble(sample = paste("sample", 1:4)),
        tibble(pos = 1:4)
    ),
    val = runif(16)
)

ggplot(df, aes(x = pos, y = sample)) +
    geom_line() +
    geom_point(aes(fill = val), pch = 21, size = 7) +
    scale_fill_steps(low = "white", high = "black")

ggplot(df, aes(x = pos, y = sample)) +
    geom_line() +
    geom_point(aes(fill = val), pch = 21, size = 7) +
    scale_fill_steps(low = "white", high = "black", limits = c(0, 1))

Created on 2020-05-22 by the reprex package (v0.3.0)

Activity

  1. yutannihilation commented on Jun 13, 2020

    @yutannihilation
    Member

    Thanks, confirmed. This seems a bug that scale_fill_steps() doesn't limit the fill scale. I think the first and last circle below should be grayed out.

    library(ggplot2)
    library(patchwork)
    
    df <- data.frame(x = 0:4)
    
    p <- ggplot(df, aes(x, 0)) +
      geom_point(aes(fill = x), pch = 21, size = 80)
    
    p1 <- p +
      scale_fill_steps(low = "white", high = "red", limits = c(1, 3)) +
      ggtitle("step")
    p2 <- p +
      scale_fill_gradient(low = "white", high = "red", limits = c(1, 3)) +
      ggtitle("gradient")
    
    p1 / p2

    Created on 2020-06-13 by the reprex package (v0.3.0)

  2. thomasp85 commented on Jun 13, 2020

    @thomasp85
    Member

    This is intentional. Binning scales squashes out of bounds values by default, whereas continuous scales censor them

  3. yutannihilation commented on Jun 13, 2020

    @yutannihilation
    Member

    Aw, I see. Sorry, I misunderstood the behaviour.

    library(ggplot2)
    
    df <- data.frame(x = 0:4)
    
    ggplot(df, aes(x, 0)) +
      geom_point(aes(fill = x), pch = 21, size = 80) +
      scale_fill_steps(low = "white", high = "red", limits = c(1, 3), oob = scales::censor)

    Created on 2020-06-13 by the reprex package (v0.3.0)

    Do you think this issue is something that we should fix? I'm wondering if we need some tweaks like what #4006 did for contours.

  4. Shians commented on Jun 14, 2020

    @Shians
    Author

    The oob behaviour is easily changeable. But the spacing in the binned scale legend is unexpected and the fix is not intuitive. I've found that setting the limits to slightly offset from my desired limits fixes this. Still, it's not clear why the binned legend spaces itself in this strange way.

    library(ggplot2)
    
    df <- data.frame(x = 0:4)
    
    ggplot(df, aes(x, 0)) +
      geom_point(aes(fill = x), pch = 21, size = 80) +
      scale_fill_steps(low = "white", high = "red", limits = c(1.001, 2.999))

    Created on 2020-06-14 by the reprex package (v0.3.0)

  5. added this to the ggplot2 3.3.4 milestone on Mar 25, 2021
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

    bugan unexpected problem or unintended behaviorscales 🐍

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions