# TODO: Add comment
# 
# Author: E.Korsching Mar 7, 2012
###############################################################################


errBar.plot <- function(xmean, xsd, ylim=c(0, max(CI.H)), flag="h", e.width=5, barcol="#0000c0", errcol="orange", names.arg=NULL, ...){
	# flag: h: plot upper, l: plot lower, b: plot both error indicator(s)
	# e.width: produce fraction width of horizontal err line
	err.H <- xmean+xsd		# upper err bar
	err.L <- xmean-xsd		# lower err bar
	#
	par(mar=c(5, 4, 4, 11)+.1, xpd=TRUE)
	xvals <- as.numeric(unlist(barplot(height=mean, ylim=ylim, col=barcol, names.arg=names.arg, ...)))	# Plot bars
	# construct width of err line
	dd <- range(unlist(xvals))[2]/length(xvals)
	dd <- dd-dd/e.width
	# Draw error bars
	if(flag=="b" | flag=="h"){
		segments(xvals, mean, xvals, err.H, col=errcol)   #line
		segments(xvals-dd, err.H, xvals+dd, err.H, col=errcol)   #top
	}
	if(flag=="b" | flag=="l"){
		segments(xvals, mean, xvals, err.L, col=errcol)
		segments(xvals-dd, err.L, xvals+dd, err.L, col=errcol)
	}
	#draw basis
	segments(xvals[1], 0, xvals[length(xvals)], 0, col=errcol)
	#
	return(list(xvals,range(xmean)))
}


#ab <- errBar.plot(mean=c(1,3,4,3.5), std=c(0.5,0.8,0.2,0.4),
#	ylim=c(0,max(c(1,3,4,3.5)+1.1*c(0.5,0.8,0.2,0.4))), flag="h", e.width=1.3, barcol=rep(c("red","blue"),2), errcol="black",
#	names.arg=NULL, ylab="dCT", space=rep(c(1.7,0.7),2), cex.axis=1.5)	#space+cex.axis will be passed through



