0

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:

enter image description here

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()
Bents
  • 23
  • 5
  • 2
    By "bars" you mean one bar (ie, you want to plot on color gradient bar)? And how is this different from your [previous question](https://stackoverflow.com/questions/48645666/how-to-make-a-gradual-color-bar-for-representation-of-risk-analysis-results-in-r)? – pogibas Feb 08 '18 at 13:45
  • Maybe [this](http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#continuous-colors) guide can help you obtain what you want. Note that variable which is set to color must be continuous. – Yatharth Malik Feb 08 '18 at 13:47
  • @PoGibas - actually I need 11 bars in one figure that's why I'm trying to use ggplot and not plot (as my previous question). Yatharth Malik by using scale_*_gradient the bars are just in one color based on there values and the gradient.. – Bents Feb 08 '18 at 13:56
  • 3
    Please provide some kind of data. Should color gradients be identical? – pogibas Feb 08 '18 at 13:57
  • 1
    Please make a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – emilliman5 Feb 08 '18 at 14:04
  • Yeah, please provide some data. If the bars only contain one color, it might be the case that you have specified the same column in `aes()` for `x =` and `fill =`. Make sure that `fill = `equals `y =` if you want the color gradient to apply to whatever is inside the bars – tifu Feb 08 '18 at 14:06
  • @PoGibas is my question is clearer with the addition? yes the colors should move from green to red – Bents Feb 08 '18 at 14:18

1 Answers1

0

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

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

enter image description here

Axeman
  • 32,068
  • 8
  • 81
  • 94