[R] Histogram with colors
S Ellison
S.Ellison at lgc.co.uk
Tue Sep 18 15:37:33 CEST 2007
>>> "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}}
More information about the R-help
mailing list