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_identity doesn't play well with guide = "legend" #2112

Closed
Henrik-P opened this issue Apr 26, 2017 · 2 comments
Closed

scale_identity doesn't play well with guide = "legend" #2112

Henrik-P opened this issue Apr 26, 2017 · 2 comments
Labels
bug an unexpected problem or unintended behavior scales 🐍

Comments

@Henrik-P
Copy link

Henrik-P commented Apr 26, 2017

To get a legend guide when using scale_identity we need to specify guide = "legend". In addition, "you'll typically* also need to supply breaks and labels" (?scale_identity).

However, when trying to get legends for color, fill, shape, size and alpha, when using scale_identity, a correct legend is obtained only for color. All other aesthetics generate either an incorrect legend (fill) or an error (shape, size and alpha).

Some data:

df <- data.frame(x = 1:2, y = 1,
                 color = c("red", "blue"), fill = c("blue",  "red"),
                 shape = 21, size = c(20, 10), alpha = c(0.8, 0.4))

Then try to get legends, without and with specification of labels and breaks.

Legends for color and fill:

library(ggplot2)
ggplot(df, aes(x = x, y = y, color = color, fill = fill, shape = shape, size = size,  alpha = alpha)) + 
   geom_point(stroke = 5) +
   scale_color_identity(guide = "legend") + 
   scale_fill_identity(guide = "legend") +
   scale_shape_identity() +
   scale_size_identity() + 
   scale_alpha_identity()

ggplot(df, aes(x = x, y = y, color = color, fill = fill, shape = shape, size = size,  alpha = alpha)) + 
   geom_point(stroke = 5) +
   scale_color_identity(guide = "legend") + 
   scale_fill_identity(guide = "legend", labels = df$fill, breaks = df$fill) + 
   scale_shape_identity() +
   scale_size_identity() +
   scale_alpha_identity()

The plot itself looks OK for all aesthetics. The legend for color is OK, but the fill legend only displays black points.


Legend for shape errors (Error: Continuous value supplied to discrete scale)

ggplot(df, aes(x = x, y = y, shape = shape)) +
   geom_point() +
   scale_shape_identity(guide = "legend")
#> Error: Continuous value supplied to discrete scale

ggplot(df, aes(x = x, y = y, shape = shape)) +
   geom_point() +
   scale_shape_identity(guide = "legend", labels = df$shape, breaks = df$shape)
#> Error: Continuous value supplied to discrete scale

Legends for size and alpha error (Error in !self$na.translate : invalid argument type)

ggplot(df, aes(x = x, y = y, size = size)) +
   geom_point() +
   scale_size_identity(guide = "legend")
#> Error in !self$na.translate: invalid argument type

ggplot(df, aes(x = x, y = y, size = size)) +
   geom_point() +
   scale_size_identity(guide = "legend", labels = df$size, breaks = df$size)
#> Error in !self$na.translate: invalid argument type

ggplot(df, aes(x = x, y = y, alpha = alpha)) +
   geom_point() +
   scale_alpha_identity(guide = "legend")
#> Error in !self$na.translate: invalid argument type

ggplot(df, aes(x = x, y = y, alpha = alpha)) +
   geom_point() +
   scale_alpha_identity(guide = "legend", labels = df$alpha, breaks = df$alpha)
#> Error in !self$na.translate: invalid argument type

By accident, I also mapped alpha while specifying a size legend, resulting in an alpha legend:

ggplot(df, aes(x = x, y = y,
               alpha = alpha)) +
  geom_point() +
  scale_size_identity(guide = "legend")


* It's unclear what 'typically' means here. At least in my examples, including or excluding labels and breaks makes no difference.

@hadley
Copy link
Member

hadley commented Jun 1, 2017

Can you please generate the reprex using the reprex package so I can see the results inline?

@hadley hadley added the reprex needs a minimal reproducible example label Jun 1, 2017
@HughParsonage
Copy link
Contributor

Output using reprex::reprex

df <- data.frame(x = 1:2, y = 1, color = c("red", "blue"), size = c(20, 10), 
  alpha = c(0.8, 0.4))
library(ggplot2)
ggplot(df, aes(x = x, y = y, color = color, size = size)) + geom_point() + scale_size_identity(guide = "legend")
#> Error in !self$na.translate: invalid argument type

@hadley hadley added bug an unexpected problem or unintended behavior scales 🐍 and removed reprex needs a minimal reproducible example labels Oct 30, 2017
@hadley hadley closed this as completed in 60293f0 Oct 30, 2017
@lock lock bot locked as resolved and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior scales 🐍
Projects
None yet
Development

No branches or pull requests

3 participants