Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add different text to each panel in lattice

Tags:

r

lattice

I would like to add different text to each panel in xyplot in lattice.

res<- xyplot(CumSpec ~ CumTotal | Site, data=data1, index.cond=list(c(1,2,3)),layout = c(3,1,1), aspect = 1,
         axis=axis.overlap, origin=0, xlab="Total number of individuals", ylab="Total number of species",
         between = list(x = 0), 
         scales=list(tick.number = 8, cex = .9, x=list(alternating=1), x=list(rot=90)),
         par.settings = my.settings,
         par.strip.text=list(col="white", font=2),
panel = function(x, y) {
panel.xyplot(x, y)

panel.abline(lm(y ~ x), lwd = 0.5, lty=2)
panel.text(400, 4.6, label="R=0.334", font=1)
}) 
res

I have tried to use panel.text but it adds the label to every panel. Does anyone know how to achieve this, please? your help would be appreciated.

like image 769
user48386 Avatar asked May 21 '14 10:05

user48386


1 Answers

The basic strategy you want is to first come up with a character vector, where each element in the vector is the text you want on a particular panel. Then you can use the panel.number() function to chose a different element of the character vector for each panel. Here is a simple example:

library(lattice)
X<-rnorm(100)
Y<-rnorm(100)
Z<-c(rep("A",50),rep("B",50))
df1<-data.frame(X,Y,Z)

MyText<-c("Panel 1 Text", "Panel 2 Text")

xyplot(X~Y|Z, data=df1,
   panel=function(x, y,...){
   panel.xyplot(x,y,...)
   panel.text(0,0,labels=MyText[panel.number()]) }
 )

You could use this strategy for anything you want to change from panel to panel (e.g your x- and y-positions for the labels, colors, pch values, etc).

like image 186
John Paul Avatar answered Oct 11 '22 17:10

John Paul