[R] Histogram color

David Winsemius dwinsemius at comcast.net
Thu Mar 4 15:16:15 CET 2010


On Mar 4, 2010, at 7:41 AM, Ashta wrote:

> In a  histogram , is it possible to have different colors?
> Example. I generated
>
> x <- rnorm(1000000)
> hist(x)
>
> I want the histogram to have different colors based on the following  
> condition
> mean(x)+sd(x)   with red color and  mean(x) - sd(x) with red color as
> well. The  middle  one with blue color.

bkrs <- hist(x); hist(x, col= ifelse(bkrs$mids< 0-1,"red", ifelse(bkrs 
$mids> 0+1, "blue", "black" ) ) )

This has the side effect of first plotting a "histogram" with bars  
that have default fill but then gathers the values in an object bkrs  
that is used to assign colors based on midpoints. Following your lead,  
I used the default values for mean and SD, so filling them in is left  
as an an exercise for the poster.


> Is it possible to do that in R?
> Thanks
>
> ______________________________________________
> 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list