R-使用矩阵布局绘制多个栅格 [英] R - plotting multiple rasters using matrix layout

查看:99
本文介绍了R-使用矩阵布局绘制多个栅格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R(Win64)中,我试图使用layout()命令以及定义布局的矩阵在单个绘图窗口中绘制栅格图像和直方图的组合.这是一些带有简化数据的示例代码:

In R (Win64), I'm trying to plot a combination of raster images and histograms in a single plot window using the layout() command with a matrix defining the layout. Here's some sample code with simplified data:

library(raster)

r <- raster(ncols=5, nrows=5, xmn=1, xmx=5, ymn=1, ymx=5)
rast1 <- rasterize(expand.grid(1:5,1:5), r, rnorm(25))
rast2 <- rasterize(expand.grid(1:5,1:5), r, rnorm(25))
rast3 <- rasterize(expand.grid(1:5,1:5), r, rnorm(25))

layout(matrix(c(1,2,3,4,1,2,3,5,1,2,3,6), 3, 4, byrow=T))
layout.show(6)

plot(rast1, axes=F, ann=F, legend=F, box=F, useRaster=T)
plot(rast2, axes=F, ann=F, legend=F, box=F, useRaster=T)
plot(rast3, axes=F, ann=F, legend=F, box=F, useRaster=T)
hist(rnorm(100), ann=F, axes=F)
hist(rnorm(100), ann=F, axes=F)
hist(rnorm(100), ann=F, axes=F)

如您所见,我正在尝试绘制三个栅格图像(rast1,rast2,rast3),它们分别跨越1列和3行,旁边有3个直方图,每个直方图跨越1列和1行. layout.show()命令给出了这个想法.

As you can see, I'm trying to plot three raster images (rast1, rast2, rast3) that span 1 column and 3 rows each, with 3 histograms beside them, each of which spans 1 column and 1 row. The layout.show() command gives the idea.

当我运行此代码时,似乎第一个绘图(光栅)命令也重置了绘图窗口的布局,导致所有后续绘图以标准3x4网格进行绘图(第5个绘图现在与第一个绘图重叠).布局设置听起来不错,因为我可以在正确的布局中绘制六个直方图.但是光栅图使事情变得混乱了.

When I run this code, it seems like the first plot (raster) command also resets the layout of the plot window, causing all subsequent plots to plot in a standard 3x4 grid (with the 5th plot now overlapping the first). The layout setup seems sound, as I can plot six histograms in the proper layout. But the raster plots mess things up.

我怀疑{raster}中的plot()命令有什么与layout()命令搞混的,但是我不知道为什么或如何.还有其他方法可以实现这种布局吗?另一个基于栅格的命令?有什么方法可以重置栅格图之间的布局?

I suspect there is something about the plot() command in {raster} that is messing with the layout() command, but I have no idea why or how. Is there some other way to achieve this layout? Another raster-based command? Is there some way to reset the layout between raster plots?

谢谢.

推荐答案

此处S4方法和layout()之间可能存在问题.作为一种解决方法,似乎可以用image()替换plot().代替使用plot(rast1,axes = F,ann = F,legend = F,box = F,useRaster = T),使用image(rast1,axes = F,ann = F,asp = 1)获得正方形图像图.

There may be an issue between S4 methods and layout() here. As a workaround, it seems to work to replace plot() with image(). Instead of plot(rast1, axes=F, ann=F, legend=F, box=F, useRaster=T), use image(rast1, axes=F, ann=F, asp=1) to get square image maps.

这篇关于R-使用矩阵布局绘制多个栅格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆