Superposing mean line to xyplot

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Superposing mean line to xyplot

Niccolò Bassani
Dear R-users,
I'm using lattice package and function xyplot for the first time so
you will excuse me for my inexperience. I'm facing quite a simple
problem but I'm having troubles on how to solve it, I've read tons of
old mails in the archives and looked at some slides from Deepayan
Sarkar but still can not get the point.

This is the context. I've got data on 9 microRNAs, each miRNA has been
measured on three different arrays and on each array I have 4
replicates for each miRNA, which sums up to a total of 108
measurements. I've the suspect that measurement on the first array are
systematically lower than the others so I wanted to draw some line
plot where each panel correspond to a miRNA, and each line correspond
to one of the four replicates (that is: first replicate of miRNA A on
array 1 must be connected to first replicate of miRNA A on array 2 and
so on), so that for each panel there are 4 series of three points
connected by a line/segment. I've done this easily with lattice doing
this:

array = rep(c("A","B","C"),each = 36) # array replicate
spot =  rep(1:4,27) # miRNA replicate on each array
miRNA = rep(rep(paste("miRNA",1:9,sep="."),each=4),3) # miRNA label
exprs = rnorm(mean=2.8,n = 108) # intensity
data = data.frame(miRNA,array,spot,exprs)
xyplot(exprs ~ array|miRNA,data=data,type="b",groups=spot,xlab="Array",ylab
= "Intensity",col="black",lty=2:5,scales = list(y = list(relation =
"free")))

Now, I want to superpose to each panel an other series of three points
connected by a line, where each point represent the mean of the four
replicates of the miRNA on each array, a sort of mean line. I've tried
using the following, but it's not working as expected:

xyplot(exprs ~ array|miRNA,data=array,type="b",groups=spot,xlab="Array",ylab
= "Intensity",col="black",lty=2:5,scales = list(y = list(relation =
"free")), panel = function(x,y,groups,subscripts){
        panel.xyplot(x,y,groups=groups,subscripts=subscripts)
        panel.superpose(x,y,panel.groups=panel.average,groups=groups,subscripts=subscripts)
})

This is maybe a silly question and possibly there's a trivial way to
do it, but I can not figure it out.

Thanx for any help.

niccolò

______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|

Superposing mean line to xyplot

Niccolò Bassani
Dear R-users,
I'm using lattice package and function xyplot for the first time so
you will excuse me for my inexperience. I'm facing quite a simple
problem but I'm having troubles on how to solve it, I've read tons of
old mails in the archives and looked at some slides from Deepayan
Sarkar but still can not get the point.

This is the context. I've got data on 9 microRNAs, each miRNA has been
measured on three different arrays and on each array I have 4
replicates for each miRNA, which sums up to a total of 108
measurements. I've the suspect that measurement on the first array are
systematically lower than the others so I wanted to draw some line
plot where each panel correspond to a miRNA, and each line correspond
to one of the four replicates (that is: first replicate of miRNA A on
array 1 must be connected to first replicate of miRNA A on array 2 and
so on), so that for each panel there are 4 series of three points
connected by a line/segment. I've done this easily with lattice doing
this:

array = rep(c("A","B","C"),each = 36) # array replicate
spot =  rep(1:4,27) # miRNA replicate on each array
miRNA = rep(rep(paste("miRNA",1:9,sep="."),each=4),3) # miRNA label
exprs = rnorm(mean=2.8,n = 108) # intensity
data = data.frame(miRNA,array,spot,exprs)
xyplot(exprs ~ array|miRNA,data=data,type="b",groups=spot,xlab="Array",ylab
= "Intensity",col="black",lty=2:5,scales = list(y = list(relation =
"free")))

Now, I want to superpose to each panel an other series of three points
connected by a line, where each point represent the mean of the four
replicates of the miRNA on each array, a sort of mean line. I've tried
using the following, but it's not working as expected:

xyplot(exprs ~ array|miRNA,data=array,type="b",groups=spot,xlab="Array",ylab
= "Intensity",col="black",lty=2:5,scales = list(y = list(relation =
"free")), panel = function(x,y,groups,subscripts){
       panel.xyplot(x,y,groups=groups,subscripts=subscripts)
       panel.superpose(x,y,panel.groups=panel.average,groups=groups,subscripts=subscripts)
})

This is maybe a silly question and possibly there's a trivial way to
do it, but I can not figure it out.

Thanx for any help.

niccolò

______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|

Re: Superposing mean line to xyplot

djmuseR
In reply to this post by Niccolò Bassani
Hi:

Here's one way to do it, adding the latticeExtra package:

array = rep(c("A","B","C"),each = 36) # array replicate
spot =  rep(1:4,27) # miRNA replicate on each array
miRNA = rep(rep(paste("miRNA",1:9,sep="."),each=4),3) # miRNA label
exprs = rnorm(mean=2.8,n = 108) # intensity
dat = data.frame(miRNA,array,spot,exprs)

library(latticeExtra)
p0 <- xyplot(exprs ~ array|miRNA, data=dat, type="b", groups = spot,
          xlab="Array", ylab = "Intensity", col="black", lty = 2:5,
          scales = list(y = list(relation = "free"))
      )
p1 <- xyplot(exprs ~ array|miRNA, data=dat, type="a",
          xlab="Array", ylab = "Intensity", col="red", lty = 1,
          lwd = 2, scales = list(y = list(relation = "free"))
      )

p0 + p1

You can also write a panel function to do this if you wish.

HTH,
Dennis

2011/10/10 Niccolò Bassani <[hidden email]>:

> Dear R-users,
> I'm using lattice package and function xyplot for the first time so
> you will excuse me for my inexperience. I'm facing quite a simple
> problem but I'm having troubles on how to solve it, I've read tons of
> old mails in the archives and looked at some slides from Deepayan
> Sarkar but still can not get the point.
>
> This is the context. I've got data on 9 microRNAs, each miRNA has been
> measured on three different arrays and on each array I have 4
> replicates for each miRNA, which sums up to a total of 108
> measurements. I've the suspect that measurement on the first array are
> systematically lower than the others so I wanted to draw some line
> plot where each panel correspond to a miRNA, and each line correspond
> to one of the four replicates (that is: first replicate of miRNA A on
> array 1 must be connected to first replicate of miRNA A on array 2 and
> so on), so that for each panel there are 4 series of three points
> connected by a line/segment. I've done this easily with lattice doing
> this:
>
> array = rep(c("A","B","C"),each = 36) # array replicate
> spot =  rep(1:4,27) # miRNA replicate on each array
> miRNA = rep(rep(paste("miRNA",1:9,sep="."),each=4),3) # miRNA label
> exprs = rnorm(mean=2.8,n = 108) # intensity
> data = data.frame(miRNA,array,spot,exprs)
> xyplot(exprs ~ array|miRNA,data=data,type="b",groups=spot,xlab="Array",ylab
> = "Intensity",col="black",lty=2:5,scales = list(y = list(relation =
> "free")))
>
> Now, I want to superpose to each panel an other series of three points
> connected by a line, where each point represent the mean of the four
> replicates of the miRNA on each array, a sort of mean line. I've tried
> using the following, but it's not working as expected:
>
> xyplot(exprs ~ array|miRNA,data=array,type="b",groups=spot,xlab="Array",ylab
> = "Intensity",col="black",lty=2:5,scales = list(y = list(relation =
> "free")), panel = function(x,y,groups,subscripts){
>        panel.xyplot(x,y,groups=groups,subscripts=subscripts)
>        panel.superpose(x,y,panel.groups=panel.average,groups=groups,subscripts=subscripts)
> })
>
> This is maybe a silly question and possibly there's a trivial way to
> do it, but I can not figure it out.
>
> Thanx for any help.
>
> niccolò
>
> ______________________________________________
> [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.
Reply | Threaded
Open this post in threaded view
|

Odp: Superposing mean line to xyplot

PIKAL Petr
In reply to this post by Niccolò Bassani
Hi

>
> Dear R-users,
> I'm using lattice package and function xyplot for the first time so
> you will excuse me for my inexperience. I'm facing quite a simple
> problem but I'm having troubles on how to solve it, I've read tons of
> old mails in the archives and looked at some slides from Deepayan
> Sarkar but still can not get the point.
>
> This is the context. I've got data on 9 microRNAs, each miRNA has been
> measured on three different arrays and on each array I have 4
> replicates for each miRNA, which sums up to a total of 108
> measurements. I've the suspect that measurement on the first array are
> systematically lower than the others so I wanted to draw some line
> plot where each panel correspond to a miRNA, and each line correspond
> to one of the four replicates (that is: first replicate of miRNA A on
> array 1 must be connected to first replicate of miRNA A on array 2 and
> so on), so that for each panel there are 4 series of three points
> connected by a line/segment. I've done this easily with lattice doing
> this:
>
> array = rep(c("A","B","C"),each = 36) # array replicate
> spot =  rep(1:4,27) # miRNA replicate on each array
> miRNA = rep(rep(paste("miRNA",1:9,sep="."),each=4),3) # miRNA label
> exprs = rnorm(mean=2.8,n = 108) # intensity
> data = data.frame(miRNA,array,spot,exprs)
> xyplot(exprs ~
array|miRNA,data=data,type="b",groups=spot,xlab="Array",ylab
> = "Intensity",col="black",lty=2:5,scales = list(y = list(relation =
> "free")))
>
> Now, I want to superpose to each panel an other series of three points
> connected by a line, where each point represent the mean of the four
> replicates of the miRNA on each array, a sort of mean line. I've tried
> using the following, but it's not working as expected:
>
> xyplot(exprs ~
array|miRNA,data=array,type="b",groups=spot,xlab="Array",ylab
> = "Intensity",col="black",lty=2:5,scales = list(y = list(relation =
> "free")), panel = function(x,y,groups,subscripts){
>        panel.xyplot(x,y,groups=groups,subscripts=subscripts)
>        panel.superpose
> (x,y,panel.groups=panel.average,groups=groups,subscripts=subscripts)
> })
>
> This is maybe a silly question and possibly there's a trivial way to
> do it, but I can not figure it out.

With some help I made function addLine

# based on Gabor Grothendieck's code suggestion
# adds straight lines to panels in lattice plots

addLine<- function(a=NULL, b=NULL, v = NULL, h = NULL, ..., once=F) {
tcL <- trellis.currentLayout()
k<-0
for(i in 1:nrow(tcL))
  for(j in 1:ncol(tcL))
    if (tcL[i,j] > 0) {
k<-k+1
        trellis.focus("panel", j, i, highlight = FALSE)
if (once) panel.abline(a=a[k], b=b[k], v=v[k], h=h[k], ...) else
panel.abline(a=a, b=b, v=v, h=h, ...)
        trellis.unfocus()
        }
}


addLine(h=tapply(data$exprs, miRNA, mean), once=T)

Regards
Petr


>
> Thanx for any help.
>
> niccolò
>
> ______________________________________________
> [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.
Reply | Threaded
Open this post in threaded view
|

Re: Superposing mean line to xyplot

Deepayan Sarkar
In reply to this post by Niccolò Bassani
2011/10/10 Niccolò Bassani <[hidden email]>:

> Dear R-users,
> I'm using lattice package and function xyplot for the first time so
> you will excuse me for my inexperience. I'm facing quite a simple
> problem but I'm having troubles on how to solve it, I've read tons of
> old mails in the archives and looked at some slides from Deepayan
> Sarkar but still can not get the point.
>
> This is the context. I've got data on 9 microRNAs, each miRNA has been
> measured on three different arrays and on each array I have 4
> replicates for each miRNA, which sums up to a total of 108
> measurements. I've the suspect that measurement on the first array are
> systematically lower than the others so I wanted to draw some line
> plot where each panel correspond to a miRNA, and each line correspond
> to one of the four replicates (that is: first replicate of miRNA A on
> array 1 must be connected to first replicate of miRNA A on array 2 and
> so on), so that for each panel there are 4 series of three points
> connected by a line/segment. I've done this easily with lattice doing
> this:
>
> array = rep(c("A","B","C"),each = 36) # array replicate
> spot =  rep(1:4,27) # miRNA replicate on each array
> miRNA = rep(rep(paste("miRNA",1:9,sep="."),each=4),3) # miRNA label
> exprs = rnorm(mean=2.8,n = 108) # intensity
> data = data.frame(miRNA,array,spot,exprs)
> xyplot(exprs ~ array|miRNA,data=data,type="b",groups=spot,xlab="Array",ylab
> = "Intensity",col="black",lty=2:5,scales = list(y = list(relation =
> "free")))
>
> Now, I want to superpose to each panel an other series of three points
> connected by a line, where each point represent the mean of the four
> replicates of the miRNA on each array, a sort of mean line. I've tried
> using the following, but it's not working as expected:
>
> xyplot(exprs ~ array|miRNA,data=array,type="b",groups=spot,xlab="Array",ylab
> = "Intensity",col="black",lty=2:5,scales = list(y = list(relation =
> "free")), panel = function(x,y,groups,subscripts){
>        panel.xyplot(x,y,groups=groups,subscripts=subscripts)
>        panel.superpose(x,y,panel.groups=panel.average,groups=groups,subscripts=subscripts)
> })

Is this close to what you are looking for?

xyplot(exprs ~ array | miRNA, data = data, col="black",lty=2:5,
       type="b", groups=spot, xlab="Array",
       ylab = "Intensity",
       scales = list(y = list(relation = "free")),
       panel = function(x, y, groups, subscripts, ...){
           panel.xyplot(x, y, groups=groups, subscripts=subscripts, ...)
           panel.average(x, y, col = "grey", lwd = 2, horizontal = FALSE)
       })

-Deepayan

______________________________________________
[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.