-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Guides with duplicate labels do not get merged properly #3573
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
Comments
Welcome @MartinEarle! I can't seem to reproduce this...perhaps you could pair down your example and render it using the reprex package? The reprex I was using to try to replicate your error is here: library(ggplot2)
ggplot(mpg, aes(cty, hwy, col = drv, linetype = drv)) +
geom_line() +
scale_colour_manual(
breaks = c("4", "f", "r"),
labels = c("4", "f", "r"),
values = c("red", "blue", "green")
) +
scale_linetype_manual(
breaks = c("4", "f", "r"),
labels = c("4", "f", "r"),
values = c("dashed", "solid", "solid")
) +
labs(color = "", linetype = "") |
Hi @paleolimbot! Sorry, I did not understand how Reprex worked before, but I have now recreated my issue now with the mpg dataset and have copied it below: library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.6.1
mpg$class <- factor(mpg$class, levels = c("pickup", "midsize","compact", "subcompact", "minivan", "suv", "2seater"))
ggplot(subset(mpg, class != "2seater"), aes(cty, hwy, col = class, linetype = class)) +
geom_line() +
scale_colour_manual(
breaks = c("pickup", "midsize","compact", "subcompact", "minivan", "suv"),
labels = c("pickup","medium", "small", "small", "large", "large"),
values = c("red", "blue", "green", "black", "yellow", "orange")
) +
scale_linetype_manual(
breaks = c("pickup", "midsize","compact", "subcompact", "minivan", "suv"),
labels = c("pickup","medium", "small", "small", "large", "large"),
values = c("dashed", "solid", "solid", "solid", "solid", "solid")
) +
theme(legend.position = "top")+
labs(color = "", linetype = "") Created on 2019-10-17 by the reprex package (v0.3.0) I am using R 3.6.0. |
Just simplifying this a tiny bit...the determining factor looks to be that there are two manual scales that are trying to be merged where library(ggplot2)
breaks <- c("pickup", "midsize","compact", "subcompact", "minivan", "suv", "2seater")
labels <- c("pickup","medium", "small", "small", "large", "large", "2seater")
ggplot(mpg, aes(cty, hwy, col = class, linetype = class)) +
geom_line() +
scale_colour_manual(
breaks = breaks,
labels = labels,
values = c("red", "blue", "green", "black", "yellow", "orange", "purple")
) +
scale_linetype_manual(
breaks = breaks,
labels = labels,
values = c("dashed", "solid", "solid", "solid", "solid", "solid", "solid")
) Created on 2019-10-17 by the reprex package (v0.2.1) |
Upon closer inspection, it looks like it's a general problem with duplicate labels and how guides are merged (using Lines 232 to 233 in 115c396
guide1 <- list(
key = tibble::tibble(
colour = 1:4,
.label = c("low", "low", "high", "high")
)
)
guide2 <- list(
key = tibble::tibble(
linetype = 1:4,
.label = c("low", "low", "high", "high")
)
)
ggplot2:::guide_merge.legend(guide1, guide2)$key
#> .label colour linetype
#> 1 low 1 1
#> 2 low 1 2
#> 3 low 2 1
#> 4 low 2 2
#> 5 high 3 3
#> 6 high 3 4
#> 7 high 4 3
#> 8 high 4 4 Created on 2019-10-17 by the reprex package (v0.2.1) We could (1) change the way merging happens ( As a workaround, one could recode the values before they get to ggplot2 (e.g., using |
Please don't enforce unique labels. There are valid cases for duplicated labels, e.g. making multiple labels equal to |
This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/ |
I have come across a small issue with manually selecting colour and linetype for my data. I want to manually select values for these aesthetics and relabel them. In this instance, I am showing the "treatment" from a factorial experiment with the label, and an additional factor with the colour. The linetype distinguishes the experiment from the control. Below is my code:
This results in the following plot:
As you can see, the labels for C & D are both duplicated. This does not occur if I rename the second "C" and "D" to "E" and "F", as shown below, or if I hide one of the guides (not shown).
I can provide some data to recreate this, if desired.
Thank you!
The text was updated successfully, but these errors were encountered: