【发布时间】:2017-10-22 10:54:26
【问题描述】:
我想使用条件颜色创建带有ggplot2 的图表,这样如果数据点符合另一列上的条件,它将是"green",否则,它将遵循预定义的调色板。
我在 SO 中看到的关于ggplot2 中条件颜色的答案建议使用scale_fill_manual 或scale_color_manual here 和here 手动指示颜色。
如果您有许多颜色类别要评估,或者只是想使用来自RColorBrewer 或Viridis 的那些漂亮的预定义调色板之一,这并不理想。
这是我尝试的可重现示例:
library(ggplot2)
library(RColorBrewer)
# get data
data("mtcars")
ggplot(mtcars, aes(x=qsec, y=hp, color= ifelse( cyl < 6, "green", factor(carb)) )) +
scale_colour_brewer( type = "seq", palette = "Reds") +
geom_point() +
theme_bw()
【问题讨论】:
标签: r ggplot2 colors conditional