[R] Histogram with different colors for different portions

arnholtat at appstate.edu arnholtat at appstate.edu
Sun Dec 30 18:57:38 CET 2007


Dear Rusers,
I would like to color different sections of a histogram different colors.
I have an example that was done by "brute force" given below.  Has anyone
implemented something like this in general?  If not, any suggestions/pointers
on how to write a general function to do so would be most appreciated.
Alan-
################################################################################
set.seed(13)
nnum <- rnorm(1000, 100, 10)
xbar <- mean(nnum)
SD <- sd(nnum)
BR <- seq(xbar - 4*SD, xbar + 4*SD, by = .25*SD)
# Histogram showing xbar +- sd in different colors
hist(nnum, breaks=BR, col=c(rep("red", 8), rep("blue", 4),
     rep("pink", 8), rep("blue", 4), rep("red", 8)))
################################################################################
# Histogram depicting Hinge Spread with boxplot
layout(matrix(c(1, 2)), heights=c(2, 1))
fn <- fivenum(nnum)
LH <- fn[2]
UH <- fn[4]
HS <- UH - LH
SHS <- HS/10
BR <- seq(LH - SHS*30, UH + SHS*30, SHS)
par(mar=c(0,2,3,2))
hist(nnum, breaks=BR, col=c(rep("red", 30), rep("blue", 10),
     rep("red", 30)), xlim=c(60,140), axes=FALSE, xlab="",
     ylab="", main="")
#
par(mar=c(3,2,0,2))
boxplot(nnum, col="blue", horizontal=TRUE, ylim=c(60, 140), axes=FALSE)
axis(side=1)

Alan T. Arnholt 
Professor and Assistant Chair 
Department of Mathematical Sciences 
Appalachian State University 
T:(828)262-2863 
F:(828)265-8617 
http://www1.appstate.edu/~arnholta/ 


More information about the R-help mailing list