简体   繁体   English

R:在screen()内绘制光栅图像不起作用

[英]R: plotting a raster image inside screen() is not working

I am trying to plot a rasterLayer object on a plot composed of two figures using screen 我正在尝试使用screen在由两个图形组成的绘图上绘制rasterLayer对象

First - a working case scenario when we only have one image: 首先-一个只有一个图片的工作案例:

library(raster)
f <- system.file("external/test.grd", package="raster")
rst <- raster(f)

plot(rst)
points(180000, 332000, col="red", pch=19)

Everything looks fine and the image looks like this: 一切看起来都很好,图像看起来像这样:

此搜索

However when I split the screen into two and try to plot the same figure using only half of the plotting region I get the following: 但是,当我将屏幕分成两部分并尝试仅使用一半绘图区域来绘制同一图形时,我得到以下信息:

library(raster)
f <- system.file("external/test.grd", package="raster")
rst <- raster(f)
class(rst)

figs <- rbind(c(0, 0.5, 0, 1), # Screen1
              c(0.5, 1, 0, 1)  # Screen2
              )
screenIDs <- split.screen(figs)

screen(screenIDs[1])
plot(rst)
points(180000, 332000, col="red", pch=19)

图像2

It seems like the main image of the raster shape is being drawn correctly. 看来光栅形状的主图像已正确绘制。 However for some reason it then captures the entire plotting region and adds a boundary lines on it. 但是由于某种原因,它随后会捕获整个绘图区域并在其上添加边界线。 After that subsequent calls to the plot (like points ) add points on the whole figure rather than staying in the first half of it. 之后,对图的后续调用(如points )在整个图形上添加点,而不是停留在图形的前半部分。

Any ideas how to solve or bypass this are most appreciated. 任何解决或绕过此问题的想法都将受到赞赏。

You can use layout instead of split.screen . 您可以使用layout而不是split.screen Demonstrated here, as in your example, with a simple layout of just 1 row of two equal-sized panels. 如您的示例所示,此处仅以两行相等大小的面板的一行的简单布局进行演示。 For how to get more complex layouts, have a look at ?layout 有关如何获得更复杂的布局,请参阅?layout

layout(matrix(c(1,2), 1, 2, byrow = TRUE))
plot(rst)
points(180000, 332000, col="red", pch=19)

plot(1:10,1:10) # a dummy plot to put in the right hand panel

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM