#==========================================================================================#
#==========================================================================================#
# This function adds whiskers (error bars to an existing plot). #
#------------------------------------------------------------------------------------------#
whiskers <<- function(x,y,xleft=NULL,xright=NULL,ybottom=NULL,ytop=NULL,cap=0.015
,lty = 1, lwd = 1,col,...){
#----- Make sure that x and y are present. ---------------------------------------------#
if (missing(x) || missing(y)){
cat(" X is missing",missing(x),"...","\n")
cat(" Y is missing",missing(y),"...","\n")
stop ("x and y must be provided...")
}#end if
#---------------------------------------------------------------------------------------#
#----- Save original PAR. --------------------------------------------------------------#
par.orig = par(no.readonly=FALSE)
if (missing(col)) col = par.orig$fg
#---------------------------------------------------------------------------------------#
#---------------------------------------------------------------------------------------#
# Plot the X error bars. #
#---------------------------------------------------------------------------------------#
if (! ( is.null(xleft) || is.null(xright))){
#----- Scale the cap with the total scale. ------------------------------------------#
ycoord = par.orig$usr[3:4]
smidge = 0.5 * cap * (ycoord[2] - ycoord[1])
#------------------------------------------------------------------------------------#
#----- Decide the scale depending on whether the y scale is linear or log. ----------#
if (par.orig$ylog){
ycapa = y * 10^(-smidge)
ycapz = y * 10^( smidge)
}else{
ycapa = y - smidge
ycapz = y + smidge
}#end if
#------------------------------------------------------------------------------------#
#---- Plot the main stem. -----------------------------------------------------------#
segments(xleft, y, xright, y, lty = lty, lwd = lwd,col=col,...)
#------------------------------------------------------------------------------------#
#---- Plot the caps. ----------------------------------------------------------------#
segments(xleft ,ycapa,xleft ,ycapa,lty=lty,lwd=lwd,col=col,...)
segments(xright,ycapz,xright,ycapz,lty=lty,lwd=lwd,col=col,...)
#------------------------------------------------------------------------------------#
}#end if
#---------------------------------------------------------------------------------------#
#---------------------------------------------------------------------------------------#
# Plot the Y error bars. #
#---------------------------------------------------------------------------------------#
if (! ( is.null(ybottom) || is.null(ytop))){
#----- Scale the cap with the total scale. ------------------------------------------#
xcoord = par.orig$usr[1:2]
smidge = cap * (xcoord[2] - xcoord[1])/2
#------------------------------------------------------------------------------------#
#----- Decide the scale depending on whether the x scale is linear or log. ----------#
if (par.orig$xlog) {
xcapa = x * 10^(-smidge)
xcapz = x * 10^( smidge)
}else{
xcapa = x - smidge
xcapz = x + smidge
}#end if
#------------------------------------------------------------------------------------#
#---- Plot the main stem. -----------------------------------------------------------#
segments(x,ybottom,x,ytop,lty = lty,lwd=lwd,col=col,...)
#------------------------------------------------------------------------------------#
#---- Plot the caps. ----------------------------------------------------------------#
segments(xcapa,ybottom,xcapz,ybottom,lwd=lwd,lty=lty,col=col,...)
segments(xcapa,ytop ,xcapz,ytop ,lwd=lwd,lty=lty,col=col,...)
#------------------------------------------------------------------------------------#
}#end if
#---------------------------------------------------------------------------------------#
return(invisible())
}#end function whiskers
#------------------------------------------------------------------------------------------#
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.