[R] Gradients in bar charts XXXX
Greg Snow
538280 at gmail.com
Fri Apr 13 20:16:11 CEST 2012
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:
> 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
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Gregory (Greg) L. Snow Ph.D.
538280 at gmail.com
More information about the R-help
mailing list