[R] Histogram with colors

Alberto Monteiro albmont at centroin.com.br
Mon Sep 17 22:11:51 CEST 2007


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



More information about the R-help mailing list