I'm building up a somewhat complex plot and want to specify the alpha for each point manually. I can do this with scale_alpha_identity, so far so good. But now I want to add a legend for my alpha scale. Adding a guide to scale_alpha_identity doesn't seem to work - I just get an error, even when I try adding breaks and labels as the documentation suggests: http://ggplot2.tidyverse.org/reference/scale_identity.html.
Minimal example: this produces the plot I'd like, but without a legend.
ggplot(data = iris) +
geom_point(aes(x = Sepal.Length, y = Sepal.Width,
alpha = Petal.Length / max(Petal.Length))) +
scale_alpha_identity()
Based on the documentation, I thought this would work but it doesn't:
ggplot(data = iris) +
geom_point(aes(x = Sepal.Length, y = Sepal.Width,
alpha = Petal.Length / max(Petal.Length))) +
scale_alpha_identity(breaks = c(0, 1), labels = c(0, 1), guide = 'legend')
I've also tried many other variations: passing 'colorbar' or 'legend' directly to the guide argument with and without the breaks and labels, passing guide_legend or guide_colorbar instead of strings - no luck, just different error messages. Adding a fake scale as suggested in Is there a way to add a legend for ggplot's alpha? doesn't work for me because it overrides my identity scale.