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. Is it possible to do that in R? Thanks
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
Here is another approach: x <- rnorm(100) tmp <- hist(x, plot=FALSE) plot(tmp, col='blue') tu <- par('usr') par(xpd=FALSE) clip( tu[1], mean(x) - sd(x), tu[3], tu[4] ) plot(tmp, col='red', add=TRUE) clip( mean(x) + sd(x), tu[2], tu[3], tu[4] ) plot(tmp, col='red', add=TRUE) -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Ashta > Sent: Thursday, March 04, 2010 5:42 AM > To: R help > Subject: [R] Histogram color > > 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. > 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.