[R] barplot2, gap.barplot

Marc Schwartz marc_schwartz at comcast.net
Thu Mar 1 14:58:34 CET 2007


On Thu, 2007-03-01 at 14:56 +0100, Marc A. Rohling wrote:
> Hello,
> 
> I try to handle a simple bar-plot, but it turns out to be not as simple
> as I thought.
> 
> 1) I have created a .dat-File, e.g. test.dat:
> 
> DATA	DATA-SEM
> 2.2	0.32 
> 6.2	1.30
> 12.7	1.61
> 48.6	3.08
> 4.1	0.86
> 4.5	0.32
> 1.5	1.13
> 1.2	1.08
> 
> The first row is the data represented by bars. The second row deals with
> the Standard Error of Mean. The lines correspond to time-intervals of
> experiments. 
> 
> 2) I now wrote this R-Script:
> #=====BEGIN=====
> # example.R
> 
> library(gdata)
> library(gtools)
> library(gmodels)
> library(gplots)
> library(plotrix)
> 
> data <- matrix(scan("./plot/example.dat", skip=1), ncol=2, nrow=8,
> byrow=TRUE, 
> dimnames=list(c("1", "2", "3", "4", "5", "6", "7", "8"),c("DATA",
> "DATA-SEM")))
> 
> conf_l <- data[, 1]	   		
> conf_u <- data[, 1] + data[, 2]	
> 
> op <- par(no.readonly = TRUE)		
> 
> par(lab=c(8,10,7))
> 
> barplot2(
> height=data[, 1],		
> width=1,			
> space=1,			
> col='black',
> border='black',
> angle=0,			
> density=NULL,		
> ylim=c(-2,55),			
> xpd=FALSE,			
> axes=TRUE,			
> las=1,
> ci.u=conf_u,			
> ci.l=conf_l,
> plot.ci=TRUE,
> ci.color="black",
> ci.lty="solid",
> ci.lwd=1,
> horiz=FALSE,
> main="Header",
> ylab="",
> xlab="\nduration of treatment",
> plot.grid=TRUE)
> 
> par(op)
> 
> #====END=====
> 
> As you can see, because of the 4th bar (value > 45), the other bars look
> a little bit tiny: there is too much white-space. What I need to handle
> this problem is a function to insert a gap.
> 
> I tried to use gap.barplot, but unfortunately, it cannot handle any of
> the parameters I need from the barplot2-function.
> 
> After days of missing effort, I am sick of this problem. Is there a
> solution?
> 
> Thanks for your help,
> 
> Marc

Marc,

A few comments:

1. I am not a big fan of axis gaps, as they tend to alter the perception
of the differences in the values. You will find similar comments in
books by Cleveland et al on statistical graphs.

2. For continuous data, I would similarly argue against using barplots
and consider a regular point plot with error bars. This can be done
easily with the combination of plot() and arrows() in base R graphics or
plotCI() in 'gplots'.

3. Depending on the nature of your data, if the extreme value is
representative of an important marked difference relative to the other
values, then I don't particularly find the 'look' of the plot to be
overly problematic. It does appropriately emphasize the large
difference.

On the other hand, you might want to consider using a log scale on the y
axis as an alternative to an axis gap. This would be a reasonable
approach to plotting values that have a notable difference in range.  If
you do this, note that you would need to ensure that all y values are >0
(ie. y axis range minimum, lower bounds of CI's, etc.) since:

> log10(0)
[1] -Inf


I don't have the plotrix package installed, but the docs for it indicate
that the gap.barplot() function does not return any values, such as the
bar midpoints, which is the case for barplot() and barplot2(). This
would suggest that Jim did not anticipate the need to add additional
plot components to the bars.

Lacking this, you have have to review the function source code to
ascertain how Jim is drawing the bars (presumably using rect() ) and
figure out the x axis values for the bar midpoints so that you could
then add the CI's. Of course, you would have to consider the axis gaps
here as well, making it a bit more cumbersome. As I note above, however,
I would advise against this approach.

HTH,

Marc Schwartz



More information about the R-help mailing list