Skip to content

Fix guide merging when there are repeated labels #3599

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

Merged

Conversation

paleolimbot
Copy link
Member

This is a PR to fix #3573, a bug whereby guides with repeated labels were merged incorrectly. This bug occurred because the two guides keys were merged using merge(), which only works when there are no duplicate values in the .label column.

Current behaviour:
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

Behaviour after this PR:

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
#>   colour .label linetype
#> 1      1    low        1
#> 2      2    low        2
#> 3      3   high        3
#> 4      4   high        4

In the current implementation of merging, guides are only merged when they have identical direction, key$.label, title, and name. Because key$.label has to be identical for both guides, it should be safe to use cbind() in guide_merge.guide_legend() instead of merge(). I took the opportunity to write (non-visual) tests for guide merging to make sure it didn't break anything. As always, happy to be convinced otherwise (or that this shouldn't be fixed right now).

Copy link
Member

@clauswilke clauswilke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, but maybe get one more review before merging.

@paleolimbot paleolimbot requested a review from thomasp85 October 30, 2019 12:11
Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM... I cannot predict whether we may get unexpected behaviour somewhere, but I guess time will tell 🙂

@paleolimbot paleolimbot merged commit 3870c12 into tidyverse:master Nov 4, 2019
@paleolimbot paleolimbot deleted the issue-3573-duplicate-guide-labels branch November 4, 2019 12:29
@lock
Copy link

lock bot commented May 5, 2020

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/

@lock lock bot locked and limited conversation to collaborators May 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Guides with duplicate labels do not get merged properly
3 participants