[R] can I add to a plot and auto-re-scale axes?

John Fox jfox at mcmaster.ca
Mon Oct 1 14:30:54 CEST 2001


Dear Robert,

I didn't follow exactly how the data are set up -- I don't think that a 
data frame can be composed of lists, so I must misunderstand what you've done.

But I can answer your plotting question. A general trick is to set up the 
plot to accommodate all of the data without plotting anything, by 
specifying type='n'. Then you can add the points. For example, suppose you 
have lists x and y composed of vectors:

     plot(range(unlist(x)), range(unlist(y)), type='n',
         xlab='x', ylab='y')
     for(i in seq(along=x)) points(x[[i]], y[[i]])

In your case, however, you don't seem to want to distinguish the different 
sets of points, so an even simpler approach would be to convert the lists 
into vectors to plot them directly:

     plot(unlist(x), unlist(y), xlab='x', ylab='y')

I hope that this is of some help,
  John


At 10:06 PM 30/09/2001 -0400, Robert D. Merithew wrote:
>(I'm new to R)
>
>Is there a way to add data to an existing plot, and have the plot axes
>rescaled automatically (i.e. if the new data lie outside the current
>axes) ?
>
>If not, how can I specify multiple datasets at once, so the axes are
>scaled to accomodate all sets?
>
>
>Details:
>
>(Am I using R's data structures in a reasonable way?)
>
>I have many small datasets taken under different conditions, and have
>placed the vectors of x-values into one list, and y-values into
>another.  The lists are part of a data frame:
>
>expts$x[[1]] is a vector of x-coordinates for experiment 1,
>expts$y[[3]] is a vector of y-coordinates for experiment 3, etc.
>
>I want to plot experiments 1, 4, and 7 together on a single plot, and
>have the axes automatically scaled.
>
> > plot(expts$x[[1]], expts$y[[1]])
> > points(expts$x[[4]], expts$y[[4]])
> > points(expts$x[[7]], expts$y[[7]])
>
>This works, but won't rescale if experiment 4 has a wider range than
>experiment 1.
>
>I'm also open to better ways of structuring the data, if that helps.
>
>Right now, expts$temp (for example), is a column of the data frame
>containing the temperature at which each dataset is taken.  I'd love to
>be able to ask for a plot of all datasets with (expts$temp == 25), for
>example.  Or build a vector of experiment numbers and plot them all...

-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
-----------------------------------------------------

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list