1

在这里我设置colorRedTRUE所以文本是红色的。但是当我将它设置为 时FALSE,颜色仍然是红色。

如何使文本颜色以 的值为条件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")
4

1 回答 1

8

这里有一个重要的教训。始终将命名向量传递给比例尺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")
于 2016-11-09T07:20:11.673 回答