简体   繁体   中英

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

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.

Any ideas how to solve or bypass this are most appreciated.

You can use layout instead of 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(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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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