本文介绍了ggplot - 如何将栏本身设置为通过颜色渐变进行着色(绿色 - >黄色 - >红色)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望条纹将被颜色渐变填充。
为了说清楚,我并不是说只用一种基于价值和渐变的颜色为整个酒吧着色,就像这样:

I want that the bars will be filled by the color gradient. Just to be clear, I don't mean to colored the whole bar with just one color based on the value and the gradient, like this:

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

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

I mean gradient inside the bar itself, something like this:

我想要做的是在11天内创建一种每日风险栏比例,然后添加箭头以指示每个栏上的风险。
它应该看起来像这样,我希望所有的酒吧都会像上面的酒吧图片一样着色。


What I'm trying to do is to create a kind of daily risk bar scale for 11 days and later on to add arrows to indicate the risk on each bar. It should look like this and I want that all the bars will be colored as the above bar pic.

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


推荐答案

您需要很多条。

You need lots of bars.

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 比例,但我不知道建议使用红到绿。

You can change the fill scale, but I don't advise using red to green.

这篇关于ggplot - 如何将栏本身设置为通过颜色渐变进行着色(绿色 - >黄色 - >红色)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 19:47