[R] overlap different line in a xyplot (lattice)
Peter Ehlers
ehlers at ucalgary.ca
Sat Dec 11 14:08:36 CET 2010
On 2010-12-11 03:12, Francesco Nutini wrote:
> mmmh, yes this method works...
> but I have to overlap this two graphs:
>
>> xyplot(a ~b |sites, data=dataset, col="red")
>
>> xyplot(c ~b |sites, data=dataset, col="blue")
>
>
> a, b and c are columns in the same dataset. "Sites" is also a column in
> the dataset, but it's a factorial variables.
> How can I use your method?
The idea is the same: you need to get your data
into "long" format with a grouping variable and
then use the 'groups' argument to xyplot.
Here's fake data frame (you should have provided one):
DF <- data.frame(y1 = rnorm(30),
y2 = rnorm(30) + 2,
x = rep(1:10, 3),
sites = gl(3, 10, lab=LETTERS[1:3]))
## Use the reshape2 package to melt the data:
## (or use reshape() in base R)
require(reshape2)
DF1 <- melt(DF, measure.vars = c('y1', 'y2'),
variable.name = 'grp', value.name = 'y')
## and plot:
require(lattice)
p <- xyplot( y ~ x | sites, data = DF1, groups = grp,
col = c("red", "blue"), type = "b")
print(p)
Peter Ehlers
> sorry for my ignorance!
>
> Francesco Nutini
>
> > Date: Fri, 10 Dec 2010 10:13:00 -0800
> > From: ehlers at ucalgary.ca
> > To: nutini.francesco at gmail.com
> > CC: r-help at r-project.org
> > Subject: Re: [R] [r] overlap different line in a xyplot (lattice)
> >
> > On 2010-12-10 07:04, Francesco Nutini wrote:
> > >
> > > dear [R] users,
> > > is there a way to plot different data (but with the same
> x-variables) in the same xyplot window?
> > > There are already a similar question, but the answer is not enought
> explanatory...
> >
> > Something like this?
> >
> > x <- rep(1:10, 2)
> > y1 <- rnorm(10); y2 <- rnorm(10) + 2
> > y <- c(y1, y2)
> > g <- gl(2, 10)
> > xyplot( y ~ x, groups = g, type = 'b')
> >
> > Peter Ehlers
> >
> > >
> > >
> > > Thanks a lot,
> > > Francesco
> > >
> >
More information about the R-help
mailing list