Skip to content
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

scale_fill_steps produces unevenly spaced legend when limits are set #4019

Closed
Shians opened this issue May 22, 2020 · 4 comments · Fixed by #4423
Closed

scale_fill_steps produces unevenly spaced legend when limits are set #4019

Shians opened this issue May 22, 2020 · 4 comments · Fixed by #4423
Labels
bug an unexpected problem or unintended behavior scales 🐍
Milestone

Comments

@Shians
Copy link

Shians commented May 22, 2020

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)

@yutannihilation
Copy link
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)

@thomasp85
Copy link
Member

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

@yutannihilation
Copy link
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.

@Shians
Copy link
Author

Shians commented Jun 14, 2020

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior scales 🐍
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants