FFFF
Skip to content

scale_color_gradient2 doesn't entirely respect limits argument #2230

Description

@rpruim

If midpoint is not half-way between the two values in limits, then the gradient scale extends beyond the limit that is closer to midpoint but the guide stops where limits specifies. Especially in conjunction with na.value, this can mislead the reader.

In this plot the color scale goes below what the guide displays, and values below 2 are not indicated as NA.

library(scales)
library(ggplot2)
library(dplyr)
ggplot(mapping = aes(x = Sepal.Length, y = Sepal.Width) ) +
  geom_point(data = iris, aes(color = Petal.Length), size = 2) +
  geom_point(data = iris %>% filter(Petal.Length < 2), size = 4, shape = 5, color = "red") +
  geom_point(data = iris %>% filter(Petal.Length > 6), size = 4, shape = 0, color = "red") +
  scale_color_gradient2(low = muted("green"), high = muted("navy"), 
                        mid = "gray80",
                        midpoint = 3, limits = c(2, 6), 
                        na.value = "orange")

If we change midpoint to 3, then the scale truly runs from 2 to 6 and values below 2 are
flagged.

ggplot(mapping = aes(x = Sepal.Length, y = Sepal.Width) ) +
  geom_point(data = iris, aes(color = Petal.Length), size = 2) +
  geom_point(data = iris %>% filter(Petal.Length < 2), size = 4, shape = 5, color = "red") +
  geom_point(data = iris %>% filter(Petal.Length > 6), size = 4, shape = 0, color = "red") +
  scale_color_gradient2(low = muted("green"), high = muted("blue"), 
                        mid = "gray80",
                        midpoint = 4, limits = c(2, 6), 
                        na.value = "orange")

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorscales 🐍wipwork in progress

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    0