One of the wonderful things about R/S is how easy it is to turn code into a new 
function definition.  Here is a function based on your code (with a couple of 
small modifications):
 
col.sd.hist <- function(x, cols=c('red','blue','pink'), ...) {
 xbar <- mean(x)
 SD <- sd(x)
 BR <- seq(xbar - 4*SD, xbar+4*SD, length.out = 33)
 hist(x, breaks=BR, col=rep( c(cols, cols[2:1]), c(8,4,8,4,8) ),
 xlab=deparse(substitute(x)), 
 main=paste("Histogram of", deparse(substitute(x))), ... )
}

Just run col.sd.hist(nnum) to get the same histogram.  You could then modify 
this to add different options.
 
Or here is another version that uses the built in number bin selection options 
(and possibly changes colors mid bar):
 
col.sd.hist2 <- function(x, cols=c('red','blue','pink'), ...) {
 xbar <- mean(x)
 SD <- sd(x)
 if( !require(TeachingDemos) ) stop("TeachingDemos package Required")
 histobj <- hist(x, plot=FALSE)
 histobj$xname <- deparse(substitute(x))
 plot(histobj)
 nB <- length(histobj$breaks)
 
 clipplot( rect(histobj$breaks[-nB], 0, histobj$breaks[-1], histobj$counts,
  col=cols[1]))
 clipplot( rect(histobj$breaks[-nB], 0, histobj$breaks[-1], histobj$counts,
  col=cols[2]), xlim= xbar + c(-2,2)*SD)
 clipplot( rect(histobj$breaks[-nB], 0, histobj$breaks[-1], histobj$counts,
  col=cols[3]), xlim= xbar + c(-1,1)*SD)
 invisible(histobj)
}
 
 
hope this helps,
 

________________________________

From: [EMAIL PROTECTED] on behalf of [EMAIL PROTECTED]
Sent: Sun 12/30/2007 10:57 AM
To: r-help@r-project.org
Subject: [R] Histogram with different colors for different portions



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/



        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to