test.df<-data.frame(Age=rnorm(100,25,10),
Sex=sample(c("M","F"),100,TRUE),
Marital=sample(c("D","M","S","W"),100,TRUE),
Employ=sample(c("Full Time","Part Time","Unemployed"),100,TRUE))
test.col<-list(Overall="gray",Employ=c("#1affd8","#caeecc","#f7b3cc"),
Marital=c("mediumpurple","orange","tan","lightgreen"),Sex=c("pink","lightblue"))
hierobarp(formula=Age~Sex+Marital+Employ,data=test.df,ylab="Mean age (years)",
main="Show only the final breakdown",errbars=TRUE,col=test.col$Sex)
# set up functions for 20 and 80 percentiles - must be offsets, not limits
q20<-function(x,na.rm=TRUE) return(mean(x)-quantile(x,probs=0.2,na.rm=TRUE))
q80<-function(x,na.rm=TRUE) return(quantile(x,probs=0.8,na.rm=TRUE)-mean(x))
# show the asymmetric dispersion measures
hierobarp(formula=Age~Sex+Marital+Employ,data=test.df,ylab="Mean age (years)",
main="Use median and quantiles for dispersion",mct=median,lmd=q20,umd=q80,
errbars=TRUE,col=test.col$Sex)
# start a wide plot window for this one
x11(width=10)
hierobarp(formula=Age~Sex+Marital+Employ,data=test.df,ylab="Mean age (years)",
main="Show the entire hierarchical breakdown",col=test.col,showall=TRUE,
showbrklab=TRUE,mar=c(5,4,4,8))
# example of a legend that might be included, needs a lot of space
par(xpd=TRUE)
legend(1.02,27,c("Overall","Full time","Part time","No work","Divorced",
"Married","Single","Widowed","Female","Male"),
fill=unlist(test.col))
par(xpd=FALSE,mar=c(5,4,4,2))
Run the code above in your browser using DataLab