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

gradient scale is converted to discrete scale when changing scale order #5360

Closed
BartJanvanRossum opened this issue Jul 21, 2023 · 2 comments

Comments

@BartJanvanRossum
Copy link

When changing the legend of a legend item with a gradient scale, the gradient scale is converted to a discrete scale. The example below shows this behavior for _colour_gradient2, but it also happens for other gradient scales.

If a plot has multiple scales, i.e. when reordering becomes relevant, just specifying the order of non-gradient scales, leaves the scale intact, but specifying the order of the gradient scale causes the behavior in the example below.

library(ggplot2)

set.seed(1)
df <- data.frame(
  x = runif(100),
  y = runif(100),
  z1 = rnorm(100)
)

ggplot(df, aes(x, y)) +
  geom_point(aes(colour = z1)) +
  scale_colour_gradient2()

ggplot(df, aes(x, y)) +
  geom_point(aes(colour = z1)) +
  scale_colour_gradient2() +
  guides(colour = guide_legend(order = 1))

Created on 2023-07-21 with reprex v2.0.2

@smouksassi
Copy link

use guide_colorbar

library(ggplot2)
set.seed(1)
df <- data.frame(
  x = runif(100),
  y = runif(100),
  z1 = rnorm(100)
  )
ggplot(df, aes(x, y)) +
  geom_point(aes(colour = z1,shape="A")) +
  scale_colour_gradient2() +
  guides(colour = guide_colorbar(order = 1))

Created on 2023-07-22 with reprex v2.0.2

@teunbrand
Copy link
Collaborator

Indeed, as @smouksassi indicates. This is not really a bug as the scale remains continuous but displays a discrete legend. I can see how this is surprising if you're unfamiliar with different guide types though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants