2016-11-09 4 views
1

Hier setze ich colorRed zu TRUE, so dass der Text rot ist. Aber wenn ich es auf FALSE einstelle, ist die Farbe immer noch rot.Bedingte Textfarbe in geom_text, scale_color_manual

Wie wird die Textfarbe vom Wert colorRed abhängig gemacht?

library(ggplot2) 

ann_text = data.frame(x = 1.5, y = max(mtcars$mpg), LABEL = "TEXT", colorRed = FALSE) 

ggplot(mtcars, aes(x = factor(am), y = mpg)) + geom_boxplot() + 
    geom_text(data = ann_text, aes(x = x, y = y, label = LABEL, color = colorRed)) + 
    scale_color_manual(values = c('red', 'black'), guide = "none") 

Antwort

3

Hier ist eine wichtige Lektion. Übergeben Sie immer einen benannten Vektor an values und labels in den Waagen, um das beabsichtigte Mapping sicherzustellen.

ggplot(mtcars, aes(x = factor(am), y = mpg)) + 
    geom_boxplot() + 
    geom_text(data = ann_text, aes(x = x, y = y, label = LABEL, color = colorRed)) + 
    scale_color_manual(values = c('TRUE' = 'red', 'FALSE' = 'black'), guide = "none")