Skip to content

Gradients in bar charts XXXX

4 messages · Jason Rodriguez, ilai, Jim Lemon +1 more

#
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?

Thanks,

Jason Michael Rodriguez
Data Analyst
State Housing Trust Fund for the Homeless
Georgia Department of Community Affairs
Email:  jason.rodriguez at dca.ga.gov

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Unknown outcomes chart 120410.pdf
Type: application/pdf
Size: 116248 bytes
Desc: Unknown outcomes chart 120410.pdf
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120409/5c6db8a7/attachment.pdf>
#
On Mon, Apr 9, 2012 at 12:40 PM, Jason Rodriguez
<Jason.Rodriguez at dca.ga.gov> wrote:
?rainbow ?hsv
In R "an easy way" is an ill-defined term. In the absence of actual data:

bpd <- matrix(c(1,seq(0,1,l=64),2,1,seq(0,1,l=64),5,1,seq(0,1,l=64),7),nc=3)
mycols <- c('green',rainbow(64,start=0,end=.4)[64:1],'red')
barplot(bpd,col=mycols,border=NA)

"Easy" enough ?

Cheers
#
On 04/10/2012 04:40 AM, Jason Rodriguez wrote:
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
3 days later
#
Here is one approach:

tmp <- rbinom(10, 100, 0.78)

mp <- barplot(tmp, space=0, ylim=c(0,100))

tmpfun <- colorRamp( c('green','yellow',rep('red',8)) )

mat <- 1-row(matrix( nrow=100, ncol=10 ))/100
tmp2 <- tmpfun(mat)

mat2 <- as.raster( matrix( rgb(tmp2, maxColorValue=255), ncol=10) )

for(i in 1:10) mat2[ mat[,i] >= tmp[i]/100, i] <- NA


rasterImage(mat2, mp[1] - (mp[2]-mp[1])/2, 0, mp[10] + (mp[2]-mp[1])/2, 100,
	interpolate=FALSE)

barplot(tmp, col=NA, add=TRUE, space=0)


You can tweak it to your desire.  It might look a little better if
each bar were drawn independently with interpolate=TRUE (this would
also be needed if you had space between the bars).


On Mon, Apr 9, 2012 at 12:40 PM, Jason Rodriguez
<Jason.Rodriguez at dca.ga.gov> wrote: