2016-11-09 118 views
1

這裏我將colorRed設置爲TRUE,因此文本爲紅色。但是,當我將其設置爲FALSE時,顏色仍爲紅色。geom_text中的條件文本顏色,scale_color_manual

如何使文本顏色條件的值爲colorRed

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") 

回答

3

這裏有一個重要的教訓。總是將一個已命名的向量傳遞給valueslabels,以確保預期的映射。

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")