问题
I have my plot window divided using
par(mfrow=c(2,4))
I have 7 plots and would like to use the remaining plot space to write the legend (i.e. the blank space in the bottom right hand corner)
I was wondering is there an easy way to put the legend into this position?
pdf("Plot")
par(mfrow=c(2,4), lwd=2, font=2, font.lab=2, font.axis=2, oma=c(0,0,2,0))
for(i in 1:7){
image(imagearray[,,i], axes=F, col=grey(c(0:225)/225), main= paste("Plot",i))
title("Plot", outer=T)
}
dev.off()
回答1:
You dont indicate how you are generating the legend so i use image.plot from the fields
package. For a reprodicible example i use the data from the examples in ?image
You can move title
and the placement of the legend outside of the loop. For the final plot you can use frame()
to move forward to the next plot window, and then plot the legend.
# data
x <- 10*(1:nrow(volcano))
y <- 10*(1:ncol(volcano))
par(mfrow=c(2,4), lwd=2, font=2, font.lab=2, font.axis=2, oma=c(0,0,2,0))
for(i in 1:7){
image(x, y, volcano, col = terrain.colors(100),
xlab="", ylab="", axes = FALSE)
}
title("Plot", outer=T)
frame()
fields::image.plot(x,y,volcano,
legend.only = TRUE,
legend.width = 10,
legend.mar = 15,
col = terrain.colors(100))
which produces

来源:https://stackoverflow.com/questions/28222375/positioning-legend-on-plot-divided-by-parmfrow-in-r