[R] Layout of mulitpage conditioned lattice plots

Dieter Menne dieter.menne at menne-biomed.de
Mon Dec 20 11:09:25 CET 2010



David Winsemius wrote:
> 
> 
> Here's my latest guess at what you may want:
> 
> pdf(file="multpage.pdf")
> xyplot(val~time|subj + comp, data=dt,type="l",
>          layout=c(3,5, 3),
>          skip=rep(c(rep(FALSE,13), TRUE, TRUE), 3) )
> dev.off()
> 
> 

Not really, but "skip" was the right idea. I added another idea of Deepayan
from a cited thread, first to plot all, then to update indexed parts with a
computed skip.

The code has become a bit lengthy because I added a more flexible
orphan-avoiding scheme.

Dieter



library(lattice)
# Distribute panels on page, so that each panel has the same size, 
# even on last page
# Use adjustCol to adjust colPerPage to avoid orphans on the last page
# 

# --------------------- adjustedColPerPage
-------------------------------------
adjustedColPerPage = function(colPerPage, ncols){
  # Allow for 20% or plus/minus 2
  searchRange = max(2L,as.integer(colPerPage*0.2))
  colsPerPage = (colPerPage-searchRange):(colPerPage+searchRange)
  nColLast = ncols %% colsPerPage
  nPages = (ncols %/% colsPerPage)+ as.integer(nColLast!=0)
  # Prefer solution with equal number on a page
  matchPage = which(nColLast==0)
  if (length(matchPage) >0) {
    colsPerPage[matchPage[which.min(abs(matchPage-searchRange))]]
  } else {
    colsPerPage[which.max(nColLast)] # not perfect
  }
}

# --------------------- xyPaged
----------------------------------------------
xyPaged = function(x, adjustCol = FALSE, colPerPage = 5,main=NULL) {
  nrows = nlevels(x$comp) # This is not very general
  ncols = nlevels(x$subj) # 
  if (adjustCol) # try to get an alternative layout that fits the pages
better
  {
    colPerPage = adjustedColPerPage(colPerPage,ncols)
    main = paste(main," usedCol= ",colPerPage)
  }
  p = xyplot(val~time|subj+comp, data=x,type="l",
    layout = c(colPerPage,nrows),main=main)
# http://r-project.markmail.org/thread/rcztoawll5kduw4x
  page = 1
  for (fromCol in seq(1,ncols,by=colPerPage)){
    toCol = min(fromCol+colPerPage-1,ncols)
    showCol = toCol %% colPerPage
    skip = rep(FALSE,colPerPage)
    if (showCol != 0) skip[(showCol+1):colPerPage] = TRUE
    print(update(p[fromCol:toCol],skip=skip,sub=page))
    page = page +1
  }
}

# Test 
testFrame  = expand.grid(adjustCol=c(FALSE,TRUE),
         nsubj=c(5,11,13),colPerPage=c(5,9,14) )

pdf(file="multpage.pdf")

for (i in 1:nrow(testFrame)) {
  test = testFrame[i,]
  dt = expand.grid(time=1:20,comp=LETTERS[1:3],subj=letters[1:test$nsubj])
  dt$val = rnorm(nrow(dt)) 
  with (test, xyPaged(dt,adjustCol, colPerPage,
        main=paste("nsubj=",test$nsubj, " requestedCol= ",colPerPage)))
}
dev.off()

-- 
View this message in context: http://r.789695.n4.nabble.com/Layout-of-mulitpage-conditioned-lattice-plots-tp3094581p3095284.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list