Skip to content

incorrect labels in the legend for scale_color_manual() #427

Closed
@alyst

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

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