ggplot - 如何设置条纹本身由颜色渐变(绿色 - >黄色 - >红色)着色

时间:2018-02-08 13:40:11

标签: r ggplot2 colors

我希望条形图由颜色渐变填充。 为了清楚起见,我并不是要根据值和渐变用一种颜色对整个条形图进行着色,如下所示:

scale_fill_gradient(low="green", high="red")

我的意思是栏内部的渐变,如下所示:

enter image description here

我想要做的是创建一种日常风险标尺,持续11天,之后添加箭头以指示每个条形图上的风险。 它看起来应该是这样的,我希望所有的条纹都会被上面的条形图片着色。

    lines <- data.frame(day=c("Today", "Tomorrow","Day 3","Day 4","Day 5","Day 6","Day 7","Day 8","Day 9","Day 10","Day 11"),
                    a=rep(4,times=11))
lines$a <- as.numeric(lines$a)
order=c(1:11)

ggplot(lines, aes(x=reorder(day,-order),y=a, fill = a)) + 
       geom_bar(stat = "identity") +
       coord_flip()

1 个答案:

答案 0 :(得分:0)

你需要很多酒吧。

lines <- expand.grid(day=c("Today", "Tomorrow","Day 3","Day 4","Day 5","Day 6","Day 7","Day 8","Day 9","Day 10","Day 11"),
                     x = 1:1000)
lines$day <- factor(lines$day, unique(lines$day))

ggplot(lines, aes(day, x, fill = x)) + 
    geom_bar(stat = "identity", show.legend = FALSE) +
    coord_flip() + 
    viridis::scale_fill_viridis()

您可以更改fill比例,但我不建议使用红色到绿色。

enter image description here