Bug 15522 - Barplot spacing bug
Summary: Barplot spacing bug
Status: NEW
Alias: None
Product: R
Classification: Unclassified
Component: Graphics (show other bugs)
Version: R 3.1.2
Hardware: x86_64/x64/amd64 (64-bit) OS X Mavericks
: P5 normal
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2013-10-31 17:16 UTC by David
Modified: 2015-01-01 16:23 UTC (History)
2 users (show)

See Also:


Attachments
R script to demonstrate barplot spacing bug (522 bytes, application/octet-stream)
2013-10-31 17:16 UTC, David
Details

Note You need to log in before you can comment on or make changes to this bug.
Description David 2013-10-31 17:16:53 UTC
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: ")
}
Comment 1 Peter Dalgaard 2013-10-31 18:20:36 UTC
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).
Comment 2 Marco Torchiano 2015-01-01 16:23:49 UTC
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")