Is there a simple way to plot a histogram with colors? For example, suppose I generate random points in the N(2,1) distribution: x <- rnorm(100000, mean = 2, sd = 1) Now I would like to plot the histogram: hist(x) but I would like to show the bars with x < 0 in red, and the bars with x >= 0 in lightgreen. Is there any simple way to do it? I think I can do it in two steps: x.hist <- hist(x, plot=FALSE) plot(x.hist, col=c(rep("red", 5), rep("green", 12))) but maybe a more direct way is available. Alberto Monteiro
Not really but a better plot call would be plot(x.hist,col=ifelse(x.hist$breaks<0,"red","green")) Ross Darnell -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Alberto Monteiro Sent: Tuesday, 18 September 2007 6:12 AM To: r-help at stat.math.ethz.ch Subject: [R] Histogram with colors Is there a simple way to plot a histogram with colors? For example, suppose I generate random points in the N(2,1) distribution: x <- rnorm(100000, mean = 2, sd = 1) Now I would like to plot the histogram: hist(x) but I would like to show the bars with x < 0 in red, and the bars with x >= 0 in lightgreen. Is there any simple way to do it? I think I can do it in two steps: x.hist <- hist(x, plot=FALSE) plot(x.hist, col=c(rep("red", 5), rep("green", 12))) but maybe a more direct way is available. Alberto Monteiro ______________________________________________ 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.
On 9/17/07, Alberto Monteiro <albmont at centroin.com.br> wrote:> Is there a simple way to plot a histogram with colors? > > For example, suppose I generate random points in the > N(2,1) distribution: > > x <- rnorm(100000, mean = 2, sd = 1) > > Now I would like to plot the histogram: > > hist(x) > > but I would like to show the bars with x < 0 in red, and the > bars with x >= 0 in lightgreen. Is there any simple way to > do it?You can do this with ggplot: install.packages("ggplot2") library(ggplot2) qplot(x, geom="histogram", data=data.frame(x), fill = factor(x >= 0)) You can find out more at http://had.co.nz/ggplot2. Hadley
>>> "Alberto Monteiro" <albmont at centroin.com.br> 17/09/2007 21:11:51 >>> >Is there a simple way to plot a histogram with colors? > >I think I can do it in two steps: > > x.hist <- hist(x, plot=FALSE) > plot(x.hist, col=c(rep("red", 5), rep("green", 12))) > >but maybe a more direct way is available.Not really, unless you specify breaks= in the histogram call. You will need to know the number of bins to specify the colours correctly, so you need a histogram object. But you can use the histogram object itself to choose which bars to colour: x<-rnorm(150) x.hist<-hist(x, plot=F) plot(x.hist, col=ifelse(x.hist$mids>0,"green","red")) $mids holds the midpoints of the bins, so it will always give you a vector of the right length for the colours. You can compress this into one line: plot(x.hist<-hist(x, plot=F), col=ifelse(x.hist$mids>0,"green","red")) but it's really the same number of operations, so it gains little. Steve ellison ******************************************************************* This email and any attachments are confidential. Any use, co...{{dropped}}