histb2b <- function(x1,x2, direction=1, names=NULL, breaks=NULL, xlab="",ylab="", col=NULL, border="#444444", clinecol="#444444", xlim=NULL, ylim=NULL, ...) { ### Comments: # x1 and x2: vectors of values to plot # direction: 1 x1 on top # 2 x1 on left # 3 x1 on bottom # 4 x1 on right # names: lables for the two variables # breaks: usual breakpoints for the histograms # may be the number of breaks, or the breaks themselves # col: two colors for the histograms # border: the border collor for the histogram rectangles # clinecol: color for the center line # # # version: 0.2015.0624 # # Check for defaults if( length(col)<1 ) col=c(2,4) if( length(col)>2 ) col=col[c(1,2)] if( length(names)<1 ) names=c("","") if( length(names)>2 ) names=names[c(1,2)] if(length(breaks)==0) { breaks=seq( min(x1,x2), max(x1,x2), length=31 ) } if(length(breaks)==1) { breaks=seq( min(x1,x2), max(x1,x2), length=breaks ) } # Alter items based on direction specified. if(direction==3 || direction==4) { yy=x1; x1=x2; x2=yy yy=names[1]; names[1]=names[2]; names[2]=yy } # Create the two histograms h1 = hist(x1, breaks=breaks, plot=FALSE) h2 = hist(x2, breaks=breaks, plot=FALSE) widest=max( c(h1$counts,h2$counts)*1.1 ) if(direction==1 || direction==3) { if( length(xlim)!=2 ) xlim=c( min(breaks),max(breaks) ) # Start plot plot.new() plot.window( xlim=xlim, ylim=c(-widest,widest) ) # Plot the histogram boxes for(i in 1:(length(breaks)-1)) { rect( h1$breaks[i],0, h1$breaks[i+1],-h1$counts[i], col=col[1], border=border) rect( h2$breaks[i],0, h2$breaks[i+1], h2$counts[i], col=col[2], border=border) } #Print the names text( min(breaks), -widest, names[1], pos=4 ) text( min(breaks), widest, names[2], pos=4 ) abline(h=0, col=clinecol) } else { ### direction = 2 or 4 if( length(ylim)!=2 ) ylim=c( min(breaks),max(breaks)) # Start plot plot.new() plot.window( xlim=c(-widest,widest), ylim=ylim ) # Plot the histogram boxes for(i in 1:(length(breaks)-1)) { rect( 0,h1$breaks[i], -h1$counts[i],h1$breaks[i+1], col=col[1], border=border) rect( 0,h2$breaks[i], h2$counts[i],h2$breaks[i+1], col=col[2], border=border) } # Print the names text( -widest, min(breaks), names[1], pos=4 ) text( widest, min(breaks), names[2], pos=2 ) abline(v=0, col=clinecol) } # END function }