|
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 ______________________________________________ [hidden email] 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. |
|
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 <[hidden email]> 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 > > ______________________________________________ > [hidden email] 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/ ______________________________________________ [hidden email] 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. |
|
In reply to this post by Rajarshi Guha-4
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 <[hidden email]>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 > > ______________________________________________ > [hidden email] 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]] ______________________________________________ [hidden email] 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. |
|
In reply to this post by Rajarshi Guha-4
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 [hidden email] 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 > > ______________________________________________ > [hidden email] 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. > ______________________________________________ [hidden email] 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. |
|
In reply to this post by djmuseR
Thanks to everybody for the solutions.
On Tue, Jul 27, 2010 at 3:47 AM, Dennis Murphy <[hidden email]> wrote: > 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 <[hidden email]> > 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 >> >> ______________________________________________ >> [hidden email] 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. > > -- Rajarshi Guha NIH Chemical Genomics Center ______________________________________________ [hidden email] 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. |
| Powered by Nabble | Edit this page |
