Rajarshi Guha
2010-Jul-27 02:26 UTC
[R] xyplot with all columns of a data.frame on a single plot
Hi, I have a data.frame with columns named X, D1, D2, D3 I know I can get a single plot with 3 curves by doing xyplot(D1 + D2 + D3 ~ X, data) but in some cases I might have columns D1 ... D10. Is there a way to plot all 10 columns without having to specify each individual term? (By analogy with formulae in lm, I thought, xyplot(. ~ X, data) would work, but it didn't) Thanks, -- Rajarshi Guha NIH Chemical Genomics Center
Felix Andrews
2010-Jul-27 03:40 UTC
[R] xyplot with all columns of a data.frame on a single plot
One method: dd <- do.call(make.groups, mydata[,-1]) dd$X <- mydata$X xyplot(data ~ X | which, dd) Another method: form <- paste(paste(colnames(mydata)[-1], collapse = " + "), "~ X") xyplot(as.formula(form), mydata) Yet another method: library(latticeExtra) xyplot.list(mydata[,-1], FUN = function(z, ...) xyplot(z ~ mydata$X, ...)) On 27 July 2010 12:26, Rajarshi Guha <rajarshi.guha at gmail.com> wrote:> Hi, I have a data.frame with columns named X, D1, D2, D3 > > I know I can get a single plot with 3 curves by doing > > xyplot(D1 + D2 + D3 ~ X, data) > > but in some cases I might have columns D1 ... ?D10. > > Is there a way to plot all 10 columns without having to specify each > individual term? > > (By analogy with formulae in lm, I thought, xyplot(. ~ X, data) would > work, but it didn't) > > Thanks, > > -- > Rajarshi Guha > NIH Chemical Genomics Center > > ______________________________________________ > 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. >-- Felix Andrews / ??? http://www.neurofractal.org/felix/
Dennis Murphy
2010-Jul-27 07:47 UTC
[R] xyplot with all columns of a data.frame on a single plot
Hi: Another approach might be to use the melt() function in package reshape before creating the plot with xyplot, something along the lines of the following: library(reshape) mdat <- melt(data, id = 'X') This should create a data frame with three columns: X, variable (all the D* names as factor levels) and value (stacked version of the D*s). Then use something like xyplot(value ~ X, data = mdat, groups = 'variable', ...) xyplot(value ~ X | variable, data = mdat, ...) One advantage of this approach is that you'll get the same structure out of melt() no matter how many D* columns you have; another is that the code block is small and relatively easy to remember six months from now. Here's a simple toy example: library(reshape) library(lattice) d <- data.frame(x = 1:20, y1 = rnorm(20), y2 = rnorm(20), y3 = rnorm(20)) # Reshape the data: m <- melt(d, id = 'x') # xyplot with a basic legend # melted data xyplot(value ~ x, data = m, groups = variable, auto.key = list(space = 'right', points = TRUE, lines = FALSE)) # plot from the original data xyplot(y1 + y2 + y3 ~ x, data = d, auto.key = list(space = 'right', points = TRUE, lines = FALSE)) # identical except for y label HTH, Dennis On Mon, Jul 26, 2010 at 7:26 PM, Rajarshi Guha <rajarshi.guha@gmail.com>wrote:> Hi, I have a data.frame with columns named X, D1, D2, D3 > > I know I can get a single plot with 3 curves by doing > > xyplot(D1 + D2 + D3 ~ X, data) > > but in some cases I might have columns D1 ... D10. > > Is there a way to plot all 10 columns without having to specify each > individual term? > > (By analogy with formulae in lm, I thought, xyplot(. ~ X, data) would > work, but it didn't) > > Thanks, > > -- > Rajarshi Guha > NIH Chemical Genomics Center > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Phil Spector
2010-Jul-27 13:18 UTC
[R] xyplot with all columns of a data.frame on a single plot
One of the nice features of R's formula syntax is that you can create a character string containing a formula, and pass it to the formula() function. For example: xyplot(formula(paste(paste(paste('D',1:10,sep=''),collapse='+'),'X',sep='~')),data) will do what you want. - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Mon, 26 Jul 2010, Rajarshi Guha wrote:> Hi, I have a data.frame with columns named X, D1, D2, D3 > > I know I can get a single plot with 3 curves by doing > > xyplot(D1 + D2 + D3 ~ X, data) > > but in some cases I might have columns D1 ... D10. > > Is there a way to plot all 10 columns without having to specify each > individual term? > > (By analogy with formulae in lm, I thought, xyplot(. ~ X, data) would > work, but it didn't) > > Thanks, > > -- > Rajarshi Guha > NIH Chemical Genomics Center > > ______________________________________________ > 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. >