Skip to content

Instantly share code, notes, and snippets.

@MrFlick
Last active July 28, 2016 02:43
Show Gist options
  • Save MrFlick/11146470 to your computer and use it in GitHub Desktop.
Save MrFlick/11146470 to your computer and use it in GitHub Desktop.
print.plotlist.R: prints a list of lattice plots with a layout()-like argument
print.plotlist<-function(xx, layout=matrix(1:length(xx), nrow=1), more=F) {
lty<-NULL
if ( is.matrix(layout) ) {
lyt <- layout
col.widths <- rep.int(1, ncol(lyt))
row.heights <- rep.int(1, nrow(lyt))
} else if ( is.list(layout) ) {
stopifnot(class(layout[[1]]) == "matrix")
lyt <- layout[[1]]
col.widths <- if (!is.null(layout$widths)) layout$widths else rep.int(1, ncol(lyt))
row.heights <- if (!is.null(layout$heights)) layout$heights else rep.int(1, nrow(lyt))
}
stopifnot(length(col.widths)==ncol(lty))
stopifnot(length(row.heights)==nrow(lty))
maxi <- max(lyt)
col.pts <- cumsum(c(0, col.widths))/sum(col.widths)
row.pts <- rev(cumsum(c(0, rev(row.heights)))/sum(row.heights))
for(i in 1:length(xx)) {
j <-((i-1)%%maxi)+1
wch <- which(lyt==j, arr.ind=T)
stopifnot(nrow(wch)>0)
pos <- apply(wch,2,range)
ps <- c(col.pts[pos[1,2]], row.pts[pos[2,1]+1], col.pts[pos[2,2]+1],row.pts[pos[1,1]])
print(
xx[[i]],
position = ps,
#split=c(rev(which(lyt==j, arr.ind=T)),rev(dim(lyt))),
more=ifelse(j != maxi & i<length(xx), T, more)
)
}
invisible(F)
}
xx<-list(
xyplot(1~1, main="First"),
xyplot(1~1, main="Second"),
xyplot(1~1, main="Thrid"),
xyplot(1~1, main="Fourth")
)
print.plotlist(xx, layout=matrix(1:4, nrow=2, byrow=T))
print.plotlist(xx, layout=matrix(c(1,1,2,4,3,3), nrow=2, byrow=T))
print.plotlist(xx, layout=list(matrix(1:4, nrow=2, byrow=T), widths=1:2, heights=2:1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment