我做了四个2层的栅格图,并且使用 par(mfrow=c(2,2)) 完成了非常简单的操作,但是当设置通用图例时,会使图变得一团糟,它总是与其他图重叠。我尝试更改一些参数值,但问题仍然存在。这是我的代码。
library(maps)
library(raster)
# set colour
col<- colorRampPalette(c("grey","green","green3","darkgreen"))
#set panel for four plots
par(mfrow=c(2,2),mar=c(2,2,2,2),oma=c(0,0,0,0))
#plot1
plot(novchl,col=col(20),legend =FALSE)
contour(novsst,maxpixels = 999,add=TRUE)
map(xlim=c(85, 93),ylim=c(2, 16 ),asp=1,add=TRUE,fill=TRUE,col="grey")
#plot2
plot(decchl,col=col(20),legend =FALSE)
contour(decsst,maxpixels = 999,add=TRUE)
map(xlim=c(85, 93),ylim=c(2, 16 ),asp=1,add=TRUE,fill=TRUE,col="grey")
#plot3
plot(janchl,col=col(20),legend =FALSE)
contour(jansst,maxpixels = 999,add=TRUE)
map(xlim=c(85, 93),ylim=c(2, 16 ),asp=1,add=TRUE,fill=TRUE,col="grey")
#plot4
plot(febchl,col=col(20),legend =FALSE)
contour(febsst,maxpixels = 999,add=TRUE)
map(xlim=c(85, 93),ylim=c(2, 16 ),asp=1,add=TRUE,fill=TRUE,col="grey")
reset para to single plot
par(mfrow=c(1,1),new=FALSE,mar=c(0,0,0,8))
plot(novchl,legend.only=TRUE ,legend.shrink=1, legend.width=1, zlim=c(0, 1),
axis.args=list(at=pretty(0:1), labels=pretty(0:1)),col=col(20),
legend.args=list(text='Whatever',"bottom", font=2, line=2.3))
我得到了这样的图像。请帮忙,提前致谢
最佳答案
我只是解决您问题的重叠主题,而不考虑布局中的任何其他更改(并且不解决警告消息)。为了获得 MWE,我使用了 raster 包中的 test-grid
数据。
我认为您已经快到了(标记 par
调用中的 oma
参数)...
在第一次调用 par
时,将 oma
参数中的第 4 条目设置为高于 0
的值为了在右侧留出一些空间。然后,当将参数重置为单个图时,也将该值重置为例如0
将图例放置在空白处。像这样的事情
library(maps)
library(raster)
r <- raster(system.file("external/test.grd", package="raster"))
# set colour
col<- colorRampPalette(c("grey","green","green3","darkgreen"))
#set panel for four plots
par(mfrow=c(2,2),mar=c(2,2,2,2),oma=c(0,0,0,4))
#plot1
plot(r,col=col(20),legend =FALSE)
contour(r,maxpixels = 999,add=TRUE)
map(xlim=c(85, 93),ylim=c(2, 16 ),asp=1,add=TRUE,fill=TRUE,col="grey")
#plot2
plot(r,col=col(20),legend =FALSE)
contour(r,maxpixels = 999,add=TRUE)
map(xlim=c(85, 93),ylim=c(2, 16 ),asp=1,add=TRUE,fill=TRUE,col="grey")
#plot3
plot(r,col=col(20),legend =FALSE)
contour(r,maxpixels = 999,add=TRUE)
map(xlim=c(85, 93),ylim=c(2, 16 ),asp=1,add=TRUE,fill=TRUE,col="grey")
#plot4
plot(r,col=col(20),legend =FALSE)
contour(r,maxpixels = 999,add=TRUE)
map(xlim=c(85, 93),ylim=c(2, 16 ),asp=1,add=TRUE,fill=TRUE,col="grey")
#reset para to single plot
par(mfrow=c(1,1),new=FALSE, oma=c(0,0,0,0))
plot(r,legend.only=TRUE ,legend.shrink=1, legend.width=1, zlim=c(0, 1),
axis.args=list(at=pretty(0:1), labels=pretty(0:1)),col=col(20),
legend.args=list(text='Whatever',"bottom", font=2, line=2.3))
关于r - 如何为面板中的四个图设置光栅图例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48576941/