[R] xlim with barplot

Jim Lemon jim at bitwrit.com.au
Mon Sep 23 05:43:02 CEST 2013


On 09/23/2013 12:55 PM, David Arnold wrote:
> Hi,
>
> I want to compare to barplots with same horizontal axis limits.
>
> x=c(55,56,57,58,59,60,60,60,61,62,63,64,65)
> y=c(35,40,45,50,55,60,60,60,65,70,75,80,85)
>
> par(mfrow=c(2,1))
> barplot(table(x),xlim=c(35,85))
> barplot(table(y),xlim=c(35,85))
> par(mfrow=c(1,1))
>
> But the bars disappear.
>
> <http://r.789695.n4.nabble.com/file/n4676717/Rplot.png>
>
> Any suggestions?
>
Hi David,
The first suggestion is:

  par("usr")
[1] -0.32 13.72 -0.03  3.00

Specifying the x limits as above means that the bars are floating 
somewhere off to the left of the plot and thus not visible. You are 
mistaking the labels of the bars for their position. Now, having 
admonished you like some grumpy old school teacher, I suppose I should 
do something useful:

barplot(tabulate(x,nbins=85)[35:85])
barplot(tabulate(y,nbins=85)[35:85])

This is an underhanded trick to line up the bars as I think you want 
them. I suppose you want x labels as well:

barpos<-barplot(tabulate(x,nbins=85)[35:85],names.arg=xylabels)
axis(1,at=barpos[c(6,16,26,36,46)],labels=c(40,50,60,70,80))
barplot(tabulate(y,nbins=85)[35:85],names.arg=xylabels)
axis(1,at=barpos[c(6,16,26,36,46)],labels=c(40,50,60,70,80))

Jim



More information about the R-help mailing list