-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Can't use labels argument in scale_*()
when show.limits = TRUE
#5009
Comments
I agree that this is a bit finnicky, but one way you could get the results you're after is to manually set limits, and include those limits in the breaks. library(ggplot2)
ggplot(mtcars, aes(hp, mpg, color = mpg)) +
geom_point() +
scale_color_stepsn(
colors = c("#dd3497",
"#ae017e",
"#7a0177",
"#49006a"),
limits = c(5, 35),
breaks = c(5, 15,20,25, 35),
labels = c("<15","15 text","20 text","25 text",">25")
) Created on 2022-12-15 by the reprex package (v2.0.1) |
@teunbrand, there have been several questions related to I've tried tracing into |
How the limits of a binned scale are defined is a bit awkward, as they're updated once the breaks are calculated. If Lines 1202 to 1230 in a4be39d
So let's suppose we have |
Thanks! That's helpful, though I think something else is also being done. Using code (slightly-modified here) from the recently-linked question, I cannot reproduce the default plot using Without breaks <- c(10, 100)
nbreaks <- length(breaks)
gg <- data.frame(c = c("a", "b", "c"), v = c(1,50,500)) %>%
ggplot(aes(y = c, x = "1", fill = v)) +
geom_tile()
gg + scale_fill_stepsn(colors = c("black", "red", "white"), breaks = breaks) If we use those apparent definitions for new_limits <- c(
breaks[1] + (breaks[1] - breaks[2]),
breaks[nbreaks] + (breaks[nbreaks] - breaks[nbreaks - 1])
)
new_limits
# [1] -80 190
gg + scale_fill_stepsn(colors = c("black", "red", "white"), breaks = breaks, limits = new_limits) So far I've been unable to debug |
The second example is because your first bin gets the midpoint of Relevant for the first example, and what I forgot to mention, is that the limit updating thing only kicks in if there were no manually provided breaks. You can debug ggproto methods like so: debugonce(environment(ScaleBinned$get_breaks)$f) |
I'm going to go ahead and say that including the limits in the break is the solution to the issue. library(ggplot2)
ggplot(mtcars, aes(hp, mpg, color = mpg)) +
geom_point() +
scale_color_stepsn(
colors = c("#dd3497",
"#ae017e",
"#7a0177",
"#49006a"),
breaks = c(15,20,25),
labels = c("15 text","20 text","25 text")
)+
guides(color = guide_colorsteps(show.limits = TRUE))
#> Warning: `show.limits` is ignored when `labels` are given as a character vector.
#> ℹ Either add the limits to `breaks` or provide a function for `labels`. Created on 2025-01-10 with reprex v2.1.1 |
It always gives
Reprex
Created on 2022-10-08 by the reprex package (v2.0.1)
The text was updated successfully, but these errors were encountered: