R codeplayer
2015-Jun-24 13:10 UTC
[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() -------------- next part -------------- A non-text attachment was scrubbed... Name: lattice_strips.pdf Type: application/pdf Size: 4678 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20150624/e6b7d4ac/attachment.pdf>
Duncan Mackay
2015-Jun-25 00:15 UTC
[R] R lattice : labeling of matrix groups of different size with strips
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()
R codeplayer
2015-Jun-25 11:01 UTC
[R] R lattice : labeling of matrix groups of different size with strips
Hi Duncan, thank you very much for your help. Originally, I thought that it is possible to use a different data representation and then to automatically create the plots and strips in lattice. Based on your suggestion, I could write the code for an annotated levelplot of the two groups (code is shown below). An open question is how to display the two groups with the same aspect ratio for the rows.> 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 NAlibrary(gridExtra) trellis.device(device = "pdf",file ="lattice_annotated_groups.pdf",width=8,height=5) #The aspect="fill" option was added to coerce the same height of the 2 plots #panel.text was used instead of grid.text to avoid using the latticeExtra package plot1 <- levelplot(plotMatrix[,,1], page = function(n) panel.text("group 1", x = 0.5, y = 0.96), colorkey = F, xlab = "", ylab="", aspect="fill") pm <- plotMatrix[1:3,,2] colnames(pm) <- rep("",ncol(pm)) plot2 <-levelplot(pm, page = function(n) panel.text( "group 2", x = 0.5, y = 0.96), colorkey = F, xlab = "", ylab="", aspect="fill") grid.arrange(plot1,plot2,ncol=2) dev.off() -------------- next part -------------- A non-text attachment was scrubbed... Name: lattice_annotated_groups.pdf Type: application/pdf Size: 4703 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20150625/0827d74f/attachment.pdf>