Created attachment 1504 [details] R script to demonstrate barplot spacing bug The barplot `space` does not work properly if there are two bars. This example is for a horizontal plot, but the vertical has the same issue. This particular example is supposed to center the bars vertically by adding spaces before the bars. This works perfectly unless there are 2 bars. #max bars+spaces m <- 21 #bars n <- N <- ceiling(m/2) repeat { #calc spaces s <- c(m/2-(n*2-1)/2, rep(1,n-1)) barplot(1:n,space=c(s),ylim=c(0,m),xlim=c(0,N),horiz=TRUE,main=paste(n,"Bars"),sub=paste("spaces(",paste(s,collapse=","),")",sep="")) box() n=n-1 if(n==0) break readline("Press <return> to continue: ") }

Yup, that's a bug alright. The logic in barplot.default is that if the input is a vector, we set beside <- TRUE. Then if beside is TRUE, we think we have a matrix check for length(space)==2 and interpret as (between, within) group. Presumably, we need to recheck for the matrix case first (i.e. NC>1, I expect).

The same spacing problem is still present: when two bars are specified the provided spacing are used in inverse order The following scripts highlight the problem and can be used to test resolution. # barplot with 2 bars, spaces: before first = 0.1, before second: 0.2 barplot(c(.6,.8),space=c(.1,.2),horiz=T,border=NA,beside=T) segments(0,0,1,0,col="gray",lty=2,xpd=T) ## zero line ## expected positions rect(0,0.1,.6,1.1,col=NA,border="red",lty=2,xpd=T) ## expected 1st bar position arrows(0.1,0,0.1,0.1,.07,code=3,col="red",xpd=T) text(0.1,0.05,"0.1 ",adj=c(1,0.5),cex=0.8,col="red",xpd=T) arrows(0.1,0.1,0.1,1.1,.07,code=3,col="red",xpd=T) text(0.1,0.55,"1.0 ",adj=c(1,0.5),cex=0.8,col="red") rect(0,1.3,.8,2.3,col=NA,border="red",lty=2,xpd=T) ## expected 2nd bar position arrows(0.1,1.1,0.1,1.3,.07,code=3,col="red",xpd=T) text(0.1,1.2,"0.2 ",adj=c(1,0.5),cex=0.8,col="red") arrows(0.1,1.3,0.1,2.3,.07,code=3,col="red",xpd=T) text(0.1,1.8,"1.0 ",adj=c(1,0.5),cex=0.8,col="red")