[R] Strange plotting error

Duncan Murdoch murdoch.duncan at gmail.com
Tue Feb 14 22:58:51 CET 2012

On 12-02-14 3:13 PM, Diviya Smith wrote:
> Hi there,
> I am trying to compute the autocorrelation in a dataset using R's acf
> function. ACF automatically plots the results. This works well except in
> some cases xlim doesnt work
> data<- rnorm(2000,0,1)
> acf(data,xlim=c(1,10))  # works - the plot starts at 1
> acf(data,lag=100,xlim=c(1,100))   # this does not work and the plot still
> starts at 0
> Is there another way to specify the xlim or the starting value for x? Any
> help would be most appreciated.

I think you misunderstand what "xlim" controls.  It sets the limit of 
the plot region that's guaranteed to be included, but the region is 
normally expanded.  It doesn't control what is plotted unless it falls 
outside the expanded region.

There are probably two ways to do what you want.  You can force the 
region not to expand, using xaxs="i" in the call.  This won't adjust the 
y axis to a smaller range; you'll need to do that yourself.

The other way is to manipulate the acf object to drop the lag 0 values; 
it is a pretty smart object, so you can do that using

x <- acf(data, lag=100, plot=FALSE)[1:100]  #only lags 1:100

Duncan Murdoch

More information about the R-help mailing list