How to add gradient fill on bar chart?

5.3k Views Asked by At

I have data frame (df), with column: asdfg (character) and NUMERIC (numeric).

Using

ggplot(df, aes(x = asdfg, y = NUMERIC)) + geom_bar(stat = "identity", fill= "red")

I made something like that:

enter image description here

Now I want to get bar chart like:

enter image description here

I've no idea how to do that. I've tried using scale_colour_gradient2 but it doesn't work (probably I did it wrong).

1

There are 1 best solutions below

2
On BEST ANSWER

Like this?

# generate sample data - you have this already
set.seed(1)    # for reproducible example
df <- data.frame(asdfg=unlist(strsplit("asdfg","")),NUMERIC=sample(1:5,5))

# you start here..
library(ggplot2)
ggplot(df, aes(x = asdfg, y = NUMERIC, fill=NUMERIC)) + 
  geom_bar(stat = "identity")+
  scale_fill_gradient(low="#FF8888",high="#FF0000")