I would like to center a title with respect to a plot matrix (and not the overall plot) I created with this code:
d <- 3
d2 <- d*d
layout.mat <- matrix(1:d2, byrow=TRUE, ncol=d) # plot matrix
layout.mat <- cbind(layout.mat, rep(0, d)) # space
layout.mat <- cbind(layout.mat, rep(d2+1, d)) # column on the right side
wspace <- 6*par("csi")*2.54 # width of the space in character height in cm
wside <- 3*par("csi")*2.54 # width of the right side in character height in cm
layout(layout.mat, respect=TRUE, widths=c(rep(1, d), lcm(wspace), lcm(wside)))
layout.show(d2+1)
par(mar=rep(0, 4), oma=c(4,4,6,4))
for(i in 1:d){
for(j in 1:d){
plot.new()
plot.window(xlim=c(0,1), ylim=c(0,1))
ll <- par("usr")
rect(ll[1], ll[3], ll[2], ll[4])
text(0.5, 0.5, paste("i=",i,", j=",j,sep=""), cex=1.4)
}
}
plot.new()
plot.window(xlim=c(0,1), ylim=c(0,1))
ll <- par("usr")
rect(ll[1], ll[3], ll[2], ll[4])
text(0.5, 0.5, "side", cex=1.4)
## title
mtext("This title should be centered according to the plot matrix", side=3,
line=2, outer=TRUE, adj=0.5, at=0.5)
How can I determine (with given fixed distances wspace and wside) the width of plot region so that I can subtract wspace+wside from it to determine the width of the plot matrix? The ultimate goal is to determine a precise value for adj of mtext such that the title is centered above the plot matrix and not the overall plotting region.
How about adding this code inside your
forloop:Note the change to
outer = FALSE. Here’s what it looks like, with shorter title text (the long version runs off either side in a small plot, but is still centered):Of course, this hack only works if you have an odd number of columns. A more general solution may be to alter the layout to include a short, wide horizontal region at the top, and plot text in that region directly: