Skip to content

scale_color_discrete() produces two legends #2

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

Open
friendly opened this issue Sep 14, 2023 · 3 comments
Open

scale_color_discrete() produces two legends #2

friendly opened this issue Sep 14, 2023 · 3 comments

Comments

@friendly
Copy link
Owner

There is something wrong in the code concerning legends for groups when you try to use scale_color_discrete() to set a property.
This has to do with the use of color and fill in geom_polygon().

The following example produces two legends:

data(wine)
wine.pca <- prcomp(wine, scale. = TRUE)
ggbiplot(wine.pca, obs.scale = 1, var.scale = 1,
  groups = wine.class, ellipse = TRUE, circle = TRUE) +
  scale_color_discrete(name = '') +
  theme(legend.direction = 'horizontal', legend.position = 'top')

See the result at:
https://github.com/friendly/ggbiplot/blob/master/man/figures/README-wine-biplot-1.png

@friendly friendly added the bug Something isn't working label Sep 14, 2023
@friendly
Copy link
Owner Author

Not a bug. Need to have the same name for aesthetics to be merged.

The following work:

  scale_fill_discrete(name = 'Species') +
  scale_color_discrete(name = 'Species') +

or,

  labs(fill = "Species", color = "Species") +

@friendly friendly removed the bug Something isn't working label Sep 16, 2023
@corybrunson
Copy link

I think this is the kind of hassle that drove a quiet API change in {ggplot2}, decided here. The duplication can also be relaxed by using the then-newly exposed aesthetics argument:

library(ggbiplot)
#> Loading required package: ggplot2
data(wine)
wine.pca <- prcomp(wine, scale. = TRUE)
ggbiplot(wine.pca, obs.scale = 1, var.scale = 1,
         groups = wine.class, ellipse = TRUE, circle = TRUE) +
  scale_color_discrete(name = 'Species', aesthetics = c('color', 'fill')) +
  theme(legend.direction = 'horizontal', legend.position = 'top')

Created on 2024-08-21 with reprex v2.1.0

@friendly
Copy link
Owner Author

Good point. I'll mention this somewhere in the documentation.

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

2 participants