Skip to content

incorrect labels in the legend for scale_color_manual() #427

Closed

Description

@alyst

Consider the following example:

test.df <- data.frame( a = rnorm(100), b = rnorm(100), c = sample( c('1','2','3'), 100, replace = TRUE ) )
ggplot( test.df ) + geom_text( aes( x = a, y = b, label = c, color = c ) ) + scale_color_manual(
        values = c( "1" = 'red', "2" = 'green', "3" ='blue', "4" = 'orange' ),
        labels = c(  "4" = '4', "2" = '2', "1" = '1',  "3" = '3' ) )

It's expected that in the legend you would see 1(red), 2(green), 3(blue).
Instead, labels are assigned to the colors by their order in which they are listed in labels: 4(red) 2(green) 1(blue)

What is worse, in this example:

test.df <- data.frame( a = rnorm(100), b = rnorm(100), c = sample( c('1','2','4'), 100, replace = TRUE ) )
ggplot( test.df ) + geom_text( aes( x = a, y = b, label = c, color = c ) ) + scale_color_manual(
        values = c( "1" = 'red', "2" = 'green', "3" ='blue', "4" = 'orange' ),
        labels = c(  "1" = '1', "2" = '2', "3" = '3',  "4" = '4' ) )

the order of the values and the labels is perfectly matched, but since value 3 is missing in the data, instead of
1(red), 2(green), 4(orange) we have 1(red), 2(green), 3(orange).

ggplot2 0.9, R2.14.1

Activity

  1. hadley commented on Apr 12, 2012

    @hadley
    Member

    Regardless of the other issues, do you think the legend should include all 4 values?

  2. hadley commented on Apr 12, 2012

    @hadley
    Member

    In the second example, the only problem is really the labelling of the legend, right? You asked that 4 be mapped to orange, and that's what you got.

  3. hadley commented on Apr 12, 2012

    @hadley
    Member

    And the real issue is that the names of labels are not used have no impact - labels are matched purely on position. Did using names ever work?

  4. alyst commented on Apr 12, 2012

    @alyst
    Author
    1. In my usecases it's fine to hide those labels that never appear in the data (like it is now), but it would be cool to have them shown as an option. :)
    2. Yes, the matching of the colors in the plot itself is ok.
    3. I'm not sure anymore and have no system to test, but I think in 0.8.x series the legend was working correctly.
  5. added a commit that references this issue on Apr 27, 2012
    168e88f
  6. locked as resolved and limited conversation to collaborators on Jun 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions