Benjamin M. Osborne
2005-Feb-16 22:04 UTC
[R] scaling axes when plotting multiple data sets
1) When adding additional data sets to a plot using "plot" followed by "lines", is there a way to automate the scaling of the axes to allow for all data sets to fit within the plot area? 2) I attempted to solve this by setting xlim=c(min(c(data1,data2,data3)),max(c(data1,data2,data3))) however, there are some NAs and Infs in these data sets, and min(data1) and max(data1) both return NA, as with data2 and data3. (These are time series). Thank you, Ben Osborne -- Botany Department University of Vermont 109 Carrigan Drive Burlington, VT 05405 benjamin.osborne at uvm.edu phone: 802-656-0297 fax: 802-656-0440
Dear Ben, On Wed, 16 Feb 2005 17:04:13 -0500 "Benjamin M. Osborne" <Benjamin.Osborne at uvm.edu> wrote:> > 1) When adding additional data sets to a plot using "plot" followed > by "lines", > is there a way to automate the scaling of the axes to allow for all > data sets > to fit within the plot area? >Not, to my knowledge, after the fact.> 2) I attempted to solve this by setting > xlim=c(min(c(data1,data2,data3)),max(c(data1,data2,data3))) > however, there are some NAs and Infs in these data sets, and > min(data1) and > max(data1) both return NA, as with data2 and data3. (These are time > series). >Specifying, e.g., min(data1, data2, data3, na.rm=TRUE) will get rid of the NAs, but it's not obvious that Infs should be removed, since if one is present, shouldn't max be Inf? If you want to get rid of the Infs, however, you could change them into NAs, as in data1[data1 == Inf] <- NA, and proceed as above. I hope this helps, John> Thank you, > Ben Osborne > > -- > Botany Department > University of Vermont > 109 Carrigan Drive > Burlington, VT 05405 > > benjamin.osborne at uvm.edu > phone: 802-656-0297 > fax: 802-656-0440 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html-------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada http://socserv.mcmaster.ca/jfox/
I just did "?min" and found an argument na.rm, which when TRUE causes "min" to ignore NAs. Also, "See Also" for "?min" mentions "range", which returns a 2-vector consisting of both min and max. The function "range" also accepts the na.rm argument. AND the documentation for "range" includes a simple example that seems to demonstrate exactly what you are requesting here. hope this helps. spencer graves Benjamin M. Osborne wrote:>1) When adding additional data sets to a plot using "plot" followed by "lines", >is there a way to automate the scaling of the axes to allow for all data sets >to fit within the plot area? > >2) I attempted to solve this by setting >xlim=c(min(c(data1,data2,data3)),max(c(data1,data2,data3))) >however, there are some NAs and Infs in these data sets, and min(data1) and >max(data1) both return NA, as with data2 and data3. (These are time series). > >Thank you, >Ben Osborne > > >
davidr@rhotrading.com
2005-Feb-16 22:37 UTC
[R] scaling axes when plotting multiple data sets
min, max, and range (and many other functions) take the na.rm parameter to ignore NAs, but the Infs must be removed by hand as far as I know: dat <- c(data1,data2,data3,data4) dat <- dat[(dat!=Inf)&(dat!=(-Inf))] rng <- range(dat, na.rm=TRUE) then use xlim = rng -- David Reiner -----Original Message----- From: Benjamin M. Osborne [mailto:Benjamin.Osborne at uvm.edu] Sent: Wednesday, February 16, 2005 4:04 PM To: r-help at stat.math.ethz.ch Subject: [R] scaling axes when plotting multiple data sets 1) When adding additional data sets to a plot using "plot" followed by "lines", is there a way to automate the scaling of the axes to allow for all data sets to fit within the plot area? 2) I attempted to solve this by setting xlim=c(min(c(data1,data2,data3)),max(c(data1,data2,data3))) however, there are some NAs and Infs in these data sets, and min(data1) and max(data1) both return NA, as with data2 and data3. (These are time series). Thank you, Ben Osborne -- Botany Department University of Vermont 109 Carrigan Drive Burlington, VT 05405 benjamin.osborne at uvm.edu phone: 802-656-0297 fax: 802-656-0440 ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Gabor Grothendieck
2005-Feb-16 23:41 UTC
[R] scaling axes when plotting multiple data sets
Benjamin M. Osborne <Benjamin.Osborne <at> uvm.edu> writes: : : 1) When adding additional data sets to a plot using "plot" followed by "lines", : is there a way to automate the scaling of the axes to allow for all data sets : to fit within the plot area? : : 2) I attempted to solve this by setting : xlim=c(min(c(data1,data2,data3)),max(c(data1,data2,data3))) : however, there are some NAs and Infs in these data sets, and min(data1) and : max(data1) both return NA, as with data2 and data3. (These are time series). Any of the following solutions would avoid having to calculate maximum and minimum in the first place: 1. You can first plot all the data together using type = "n" (no points are actually shown) and then add them one by one plot(c(x1,x2), c(y1,y2), type = "n") lines(x1, y1) lines(x2, y2) 2. you may be able to use matplot. See ?matplot . 3. if these are 'ts' class time series you could plot them all at once using ts.plot even if they have different time bases. See ?ts.plot 4. the 'zoo' library's plot function has facilities for plotting multiple time series all at once even if they have different time bases. See library(zoo); ?plot.zoo
On Wed, 16 Feb 2005 davidr at rhotrading.com wrote:> min, max, and range (and many other functions) take the na.rm parameter > to ignore NAs, but the Infs must be removed by hand as far as I know: > > dat <- c(data1,data2,data3,data4) > dat <- dat[(dat!=Inf)&(dat!=(-Inf))] > rng <- range(dat, na.rm=TRUE) > then use xlim = rngrange(dat[is.finite(dat)]) is clearer, I find. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595