[R] R lattice : labeling of matrix groups of different size with strips

Duncan Mackay dulcalma at bigpond.com
Thu Jun 25 02:15:07 CEST 2015


Hi

I am not sure what you want

plotMatrix
, , group1

  a b c d
1 1 0 0 0
2 1 0 0 0
3 1 1 0 0
4 0 1 0 0
5 0 1 1 0

, , group2

   a  b  c  d
1  0  0  1  0
2  0  0  1  1
3  0  0  0  1
4 NA NA NA NA
5 NA NA NA NA

If you do not want to show the NA's without giving them a different colour then here is a cludgy way of doing things

print(
levelplot(plotMatrix[1:3,,2],
          page     = function(n){
                       grid.text(paste("group2"),
                                 x = 0.5,
                                 y = 0.96,
                                 default.units = "npc",
                                 just = c("left", "bottom"),
                                 gp = gpar(fontsize = 12) )
                      },
          colorkey = F,
          xlab = "",
          ylab=""),
position = c(0.2,0,0.8,0.5), more = TRUE)
print(
levelplot(plotMatrix[,,1],
          page     = function(n){
                       grid.text(paste("group1"),
                                 x = 0.5,
                                 y = 0.96,
                                 default.units = "npc",
                                 just = c("left", "bottom"),
                                 gp = gpar(fontsize = 12) )
                      },
          colorkey = F,
          xlab = "",
         ylab=""),
position = c(0,0.5,1,1), more = FALSE)

It will depend on your device so you will have to amend the position settings of group n and size of plots.

using the page argument saves having to do a panel function 

If you wanted to have the strip that is a different matter

Regards

Duncan

PS Does this suit?

library(latticeExtra)
c(levelplot(plotMatrix[,,1],colorkey=F,xlab="",ylab=""),levelplot(plotMatrix[1:3,,2],colorkey=F,xlab="",ylab=""))

just using the defaults. have not got time to explore further
you may have to annotate groups by grid.text  with or without trellis.focus

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au 
 



-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of R codeplayer
Sent: Wednesday, 24 June 2015 23:10
To: r-help at r-project.org
Subject: [R] R lattice : labeling of matrix groups of different size with strips

In R lattice, I am trying to label predefined groups of rows in a
matrix of data with strips. Currently, the length of the strips fail
to match the different sizes of the groups as the data representation
only allows groups with the same size.

One possibility to solve this might be to suppress the display of
NAs, but I did not find any configuration to realize
this in Lattice.

The example code below shows a matrix (m) with 8 rows and 4 columns.
Group 1 contains row 1-5 and group 2 contains row 6-8. The lattice
output is attached below the code.

Thank you for your time



library(lattice)

m <- matrix(c(1,1,1,0,0,0,0,0,
            0,0,1,1,1,0,0,0,
            0,0,0,0,1,1,1,0,
            0,0,0,0,0,0,1,1),nrow=8,ncol=4)

group1 <- m[1:5,]
group2 <- m[6:nrow(m),]

plotMatrix <- array(dim=c(5,4,2))
dimnames(plotMatrix) <- list(rep("",5), c("a","b","c","d"),c("group1","group2"))
plotMatrix[,,1]<- group1
plotMatrix[1:3,,2] <- group2

trellis.device(device = "pdf",file ="lattice_strips.pdf",width=14,height=10)
print(levelplot(plotMatrix,colorkey=F,xlab="",ylab=""))
dev.off()



More information about the R-help mailing list