How to add different text to each panel in the grid

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

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 tried using panel.text, but it adds a shortcut to each panel. Does anyone know how to achieve this, please? Your help will be appreciated.

+4
source share
1 answer

The main strategy you want is to first create a character vector, where each element in the vector is the text that you want in a particular panel. Then you can use the function panel.number()to select a different symbol vector element 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()]) }
 )

, (, x- y- , , pch ..).

+4

Source: https://habr.com/ru/post/1541402/


All Articles