[R] automatically adjusting axis limits

Ben Bolker bolker at ufl.edu
Tue Oct 27 19:58:23 CET 2009




Sac-6 wrote:
> 
> Dear R users,
> 
> I am a newbie. Just switched from MATLAB. So thanks a lot for your
> patience.
> 
> I have 50000 spectra collected in field. Each spectra has two columns :
> Wavelength (56) and the actual measurement.  
> 
> Each measurement came in a different .txt file on disk (50000 files in
> total). I wrote a script that reads every spectra in a for loop and
> constructs two variables :
> 
> Wavelength (56) and Reflectance (56x50000). I would like to plot
> Reflectance vs Wavelength i.e. overlay 50000 spectra one one top of the
> other.
> 
> plot(Wavelength, Reflectance) does not work (Matlab would do it): 
> 
> Error in xy.coords(x, y, xlabel, ylabel, log) : 
> 'x' and 'y' lengths differ
> 
> 
> I then tried to construct the two matrices so that they have the same size
> (56x50000) and plot it all at once with the command "plot". This works but
> it is such a computationally inefficient way that I do not want to do this
> Why redundantly store wavelength data? Later I will have to process much
> more spectra so this is not a good practice for me.
> 
> I then decided to draw the first spectra on the first run of the for loop
> with the command "plot" and add the subsequent graphs with the command
> "lines". This works but the y-axes limits do not adjust automatically,
> leaving many spectra out of the axis limits  ;( 
> 
> I don't want to set the axis limits by hand as I need this script to be
> completely autonomous. I don't want to program lines of code to calculate
> those limits myself either.... I am sure the mighty R can do it... BUT
> HOW???? (Matlab would easily do it with a single command) 
> 
> What I need is a command that will redraw the graph by automatically
> adjusting the axis limits. I have been searching for many days on the web,
> forums and mailing list archives but I still don't know how to do it.
> Please help
> 
> thanks a lot from advance for your kindly help
> Servet
> 
> 

Are all the wavelengths the same?
The best answer is matplot(), but you need to reformat your data a bit.

for example:

files <- list.files(pattern=[something appropriate])
datlist <- lapply(files,read.table,...[other arguments such as header=TRUE])
wavelens <- datlist[[1]][,1]
datlist2 <- lapply(datlist,"[[","vals"]]  ## assuming second column is
called "vals"
datmat <- do.call(datlist2,cbind)
matplot(wavelens,datmat)

-- 
View this message in context: http://www.nabble.com/automatically-adjusting-axis-limits-tp26082508p26083141.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list