[R] Gradients in bar charts XXXX

Jim Lemon jim at bitwrit.com.au
Tue Apr 10 09:33:24 CEST 2012


On 04/10/2012 04:40 AM, Jason Rodriguez wrote:
> Hello, I have a graphics-related question:
>
> I was wondering if anyone knows of a way to create a bar chart that is colored with a three-part gradient that changes at fixed y-values. Each bar needs to fade green-to-yellow at Y=.10 and from yellow-to-red at Y=.20. Is there an option in a package somewhere that offers an easy way to do this?
>
> Attached is a chart I macgyvered together in Excel using a combination of a simple bar chart, fit line, and some drawing tools. I want to avoid doing it this way in the future by finding a way to replicate it in R.
>
> Any ideas?
>
Hi Jason,
When I first read your message, I immediately thought of using 
gradient.rect (plotrix). With a bit of recoding, the barp function (also 
in plotrix) could use gradient.rect instead of rect. The trick would be 
to create a vector of colors that would fill the tallest bar and pass 
that as "col". Only the colors that are necessary to fill each bar will 
actually be displayed. To get such a vector, stick together sequences of 
constant colors plus sequences from the color.scale function (or 
similar) for the fades. Once you have created the vector of desired 
colors, you can just pass that. I think this would be a custom function 
you could program unless lots of people leap up in joy at the thought of 
such a bar plot.

Jim



More information about the R-help mailing list