1

ここでは、テキストが赤になるように設定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")
4

1 に答える 1

8

ここで重要な教訓があります。意図したマッピングを保証するために、常に名前付きベクトルをスケールに渡しvaluesます。labels

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 に答える