Skip to content

Fix guide merging when there are repeated labels - #3599

Merged
paleolimbot merged 4 commits into
tidyverse:masterfrom
paleolimbot:issue-3573-duplicate-guide-labels
Nov 4, 2019
Merged

paleolimbot merged 4 commits into
tidyverse:masterfrom
paleolimbot:issue-3573-duplicate-guide-labels

Conversation

@paleolimbot

Copy link
Copy Markdown
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.

Details 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).

@clauswilke clauswilke left a comment

Copy link
Copy Markdown
Member

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

@thomasp85 thomasp85 left a comment

Copy link
Copy Markdown
Member

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

lock Bot commented May 5, 2020

Copy link
Copy Markdown

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