Skip to content

incorrect labels in the legend for scale_color_manual() #427

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

Closed
alyst opened this issue Mar 6, 2012 · 4 comments
Closed

incorrect labels in the legend for scale_color_manual() #427

alyst opened this issue Mar 6, 2012 · 4 comments
Milestone

Comments

@alyst
Copy link

alyst commented Mar 6, 2012

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

@hadley
Copy link
Member

hadley commented Apr 12, 2012

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

@hadley
Copy link
Member

hadley commented Apr 12, 2012

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.

@hadley
Copy link
Member

hadley commented Apr 12, 2012

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?

@alyst
Copy link
Author

alyst commented Apr 12, 2012

  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.

@hadley hadley closed this as completed in 0ce3fb1 Apr 12, 2012
kohske pushed a commit to kohske/ggplot2 that referenced this issue Apr 27, 2012
@lock lock bot locked as resolved and limited conversation to collaborators Jun 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants