Last active
February 3, 2017 22:45
-
-
Save petrelharp/f4d238d75c65a16a13afdc0ce826e54d to your computer and use it in GitHub Desktop.
Calculate heights of figure regions approprate for using layout() without space between the plots and all figures staying the same height.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
layout_heights <- function (k,dl=0,ncol=1) { | |
# to set up layout without 'dl' lines between plots | |
# use like layout(1:5,heights=layout_heights(5)) | |
if (k==1) return(1) | |
layout(matrix(seq_len(k*ncol),ncol=ncol)) # this changes par("csi") | |
ds <- dl*par("lheight")*par("csi") | |
eps=par("mai")[c(1,3)] | |
dh=(par("din")[2]-sum(eps)-(k-1)*ds)/k | |
return(c(eps[2]+dh+ds/2,rep(dh+ds,k-2),eps[1]+dh+ds/2)/par("din")[2]) | |
} | |
# Example: | |
spacing <- 1 | |
fig.rows <- 5 | |
opar <- par(mar=c(4,4,2,1)+.1,mgp=c(2.5,0.8,0)) | |
layout(matrix(1:fig.rows,ncol=1),heights=layout_heights(fig.rows,dl=spacing,ncol=1)) | |
opar2 <- par(mar=c(par("mar"),spacing/2)[c(5,2,3,4)]) | |
for (k in 1:fig.rows) { | |
lastone <- (k==fig.rows) | |
if (lastone) { par(mar=c(par("mar"),opar2$mar[1])[c(5,2,3,4)]) } | |
plot(1, xaxt=if(lastone){"s"}else{"n"}, | |
xlab=if(lastone){"x"}else{""} ) | |
par(mar=c(par("mar"),spacing/2)[c(1,2,5,4)]) | |
} | |
par(opar) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment