Gradients in bar charts XXXX

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Gradients in bar charts XXXX

Jason Rodriguez
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:  [hidden email]


______________________________________________
[hidden email] 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.

Unknown outcomes chart 120410.pdf (155K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Gradients in bar charts XXXX

ilai-2
On Mon, Apr 9, 2012 at 12:40 PM, Jason Rodriguez
<[hidden email]> 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?

?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


>
> 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:  [hidden email]
>
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Gradients in bar charts XXXX

Jim Lemon
In reply to this post by Jason Rodriguez
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Gradients in bar charts XXXX

glsnow
In reply to this post by Jason Rodriguez
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
<[hidden email]> 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:  [hidden email]
>
>
> ______________________________________________
> [hidden email] 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.
[hidden email]

______________________________________________
[hidden email] 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.