[R] Odp: Superposing mean line to xyplot

Petr PIKAL petr.pikal at precheza.cz
Tue Oct 11 08:01:52 CEST 2011


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ò
> 
> ______________________________________________
> 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