Positioning legend on plot divided by par(mfrow) in R

﹥>﹥吖頭↗ 提交于 2019-12-08 11:19:28

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!