R geom_文本中的条件文本颜色,缩放_颜色_手册

R geom_文本中的条件文本颜色,缩放_颜色_手册,r,ggplot2,geom-text,R,Ggplot2,Geom Text,这里我将colorRed设置为TRUE,因此文本为红色。但当我将其设置为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_te

这里我将
colorRed
设置为
TRUE
,因此文本为红色。但当我将其设置为
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")

这里有一个重要的教训。始终将命名向量传递给比例中的
标签
,以确保预期映射

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

这里有一个重要的教训。始终将命名向量传递给比例中的
标签
,以确保预期映射

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