# this was created in an attempt to make trellis plots in R # like those found in the lattice package, but made from scratch # to have more control for things like the ability to plot by column # rather than by row. Share and modify as you like. # Ian Taylor, June 20, 2006 nplots = 17 # can be adjusted a lot, but looks pretty silly above 100 # nrows and ncols chosen to make approximately square plot # favoring slightly more rows than cols nrows = ceiling(sqrt(nplots)) ncols = ceiling(nplots/nrows) windim = c(7,7) #if windows(a,b) is called, replace this with c(a,b) mardim = 0.7 #amount needed for axes titledim = 0.5 #amount needed for title # making a matrix to represent the positions and orders of the cells layoutmat = matrix(1:(nrows*ncols),nrows,ncols, byrow=F) # change to byrow=T if desired layoutmat[layoutmat>nplots] = 0 # zero out any cells beyond nplots layoutmat = cbind(nplots+1:nrows,layoutmat) # add entries for vertical axis layoutmat = rbind(layoutmat, c(0,nplots+nrows+1:ncols)) # add entries for horiz. axis layoutmat = rbind(c(0,rep(nplots+nrows+ncols+1,ncols)),layoutmat) # centered over plots, not true center # here's what layoutmat looks like at nplots=17 # zeros represent unused cells, repeated value # across the top has those cells merged as a single cell # [,1] [,2] [,3] [,4] [,5] #[1,] 27 27 27 27 27 #[2,] 18 1 6 11 16 #[3,] 19 2 7 12 17 #[4,] 20 3 8 13 0 #[5,] 21 4 9 14 0 #[6,] 22 5 10 15 0 #[7,] 0 23 24 25 26 nf = layout(layoutmat,respect=T, widths=c(mardim, rep((windim[1]-mardim)/ncols,ncols)), heights=c(titledim,rep((windim[2]-mardim-titledim)/nrows,nrows),mardim)) layout.show(nf) #this shows the layout in the R window ymax=1.4 nbars = 10 for(i in 1:nplots) # plot the main cells { par(mar=rep(0,4)) xvals = barplot(runif(nbars), col='grey', axes=F, xlab='', ylab='', main='', ylim=c(0,ymax)) legend('top', paste('plot ',i), bty='n') box() xlim1 = par()$usr[1:2] #saves the range of x created by barplot } for(i in 1:nrows) # plot the vertical axes { par(mar=c(0,5.1,0,0)) # 5.1 chosen by trial and error, may require modification plot(0, type='n', xaxs='i', yaxs='i', ylim=c(0,ymax), axes=F, xlab='', ylab='ylab', main='') axis(2, at = seq(0,1,0.5)) #mtext(side=2,line=3,'proportion') } for(i in 1:ncols) # plot the horizontal axes { par(mar=c(5.1,0,0,0)) # 5.1 chosen by trial and error, may require modification plot(0, type='n', xaxs='i', yaxs='i', xlim=xlim1, axes=F, xlab='xlab', ylab='',main='') axis(1, at=xvals, labels=1:nbars) #mtext(side=1,line=3,'length bin (cm)') } par(mar=c(0,0,0,0)) # add a title plot(0,0,type='n',xlab='',ylab='',main='',axes=F) text(0,0,'big title',font=2, cex=2)