[R] overlaying plots from a list of data frames

Gabor Grothendieck ggrothendieck at gmail.com
Wed Feb 4 09:59:33 CET 2009


Create a zoo object z and plot it:

library(zoo)
f <- function(x) zoo(x$data, levels(x$year)[x$year])
z <- do.call(merge, lapply(foo, f))
plot(z, screen = 1, col = 1:6, pch = 1:6, type = "o",
	ylab = "data", xlab = "year")
legend("topright", legend = 1:6, lty = 1, pch = 1:6, col = 1:6)

See ?plot.zoo and the 3 zoo vignettes (pdf documents) in zoo package.

On Tue, Feb 3, 2009 at 4:54 PM, Timothy W. Hilton <twh142 at psu.edu> wrote:
> Hello R list,
>
> I have a list of data frames, in the form (dump output for an example
> list follows message):
>
> $site1
>  year        data
> 1 2000  0.03685042
> 2 2001  0.02583885
> 6 2005  0.02480015
> 7 2006  0.03458745
>
> $site2
>  year          data
> 1 2002  4.071134e-03
> 2 2003 -4.513524e-08
> 3 2004  8.336272e-08
> 4 2005  2.270803e-08
> 5 2006  1.263597e-02
> 6 2007           NaN
>
> I would like to plot each site's data on a single plot, using a
> different color and marker for each site.  I can't figure out an
> elegant way to do this; right now I am calling plot on site1, then
> points on site2 ... siteN.  This leads to recycling the list of plot
> parameters (color, pch, etc.).
>
> The following code allows me to use
> do.call(FUN, lapply(list.of.sites, etc.
>
> if (dev.cur() > 1) FUN <- points
>  else FUN <- plot
>
> Now I would like to include several panels in one figure (there are
> actually several columns of data in the data frame).  I set up the
> window with the panels, but that messes up the dev.cur() test I am
> doing.
>
> I would be most grateful for any suggestions of a more elegant setup,
> or for a different condition to decide whether points or plot should
> be called.  Something like axis.already.exists(), but I can't figure
> out what that test should be.
>
> Many, many thanks.
> -Tim
>
> --
>
> Timothy W. Hilton
> PhD Candidate, Department of Meteorology and Atmospheric Science
> The Pennsylvania State University
> 415 Walker Building, University Park, PA   16802
> hilton at meteo.psu.edu
>
> example list of data frames:
> foo <-
> structure(list(site1 = structure(list(year = structure(1:8, .Label =
> c("2000",
> "2001", "2002", "2003", "2004", "2005", "2006", "2007"), class =
> c("ordered",
> "factor")), data = c(0.0368504167319695, 0.0258388514332078,
> 0.0432316303304793, 0.0104757403348321, -0.00942333944266443,
> 0.0248001501763526, 0.03458745330367, NaN)), .Names = c("year",
> "data"), class = "data.frame", row.names = c(NA, -8L)), site2 =
> structure(list(
>    year = structure(1:6, .Label = c("2002", "2003", "2004",
>    "2005", "2006", "2007"), class = c("ordered", "factor")),
>    data = c(0.00407113433104158, -4.51352402858745e-08,
> 8.33627186959167e-08,
>    2.27080257687498e-08, 0.0126359731744113, NaN)), .Names =
> c("year",
> "data"), class = "data.frame", row.names = c(NA, -6L)), site3 =
> structure(list(
>    year = structure(1:7, .Label = c("2000", "2001", "2002",
>    "2003", "2004", "2005", "2006"), class = c("ordered", "factor"
>    )), data = c(0.0297773868339168, 0.0212836833675155,
> 0.0301490879766663,
>    0.000944393402486824, -2.70247618264114e-09,
> -2.84624718656657e-07,
>    NaN)), .Names = c("year", "data"), class = "data.frame", row.names
> = c(NA,
> -7L)), site4 = structure(list(year = structure(1:5, .Label = c("2000",
> "2001", "2002", "2003", "2004"), class = c("ordered", "factor"
> )), data = c(0.000418478048104973, 0.00530609034999336,
> 0.020077837088374,
> 0.0326921217142046, NaN)), .Names = c("year", "data"), class =
> "data.frame", row.names = c(NA,
> -5L)), site5 = structure(list(year = structure(1:5, .Label = c("2000",
> "2001", "2003", "2004", "2005"), class = c("ordered", "factor"
> )), data = c(0.00618165243153993, 0.00303216804813299,
> -5.51581146266983e-08,
> 1.44109415817357e-09, 0.0336487468345765)), .Names = c("year",
> "data"), class = "data.frame", row.names = c(NA, -5L)), site6 =
> structure(list(
>    year = structure(1:6, .Label = c("2001", "2002", "2003",
>    "2004", "2005", "2006"), class = c("ordered", "factor")),
>    data = c(-6.33889022834333e-10, 1.34668283090261e-08,
> 4.17885398551684e-08,
>    0.00202872966966414, -3.82189201782033e-09, NaN)), .Names =
> c("year",
> "data"), class = "data.frame", row.names = c(NA, -6L))), .Names =
> c("site1",
> "site2", "site3", "site4", "site5", "site6"))
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list