用光栅上的点绘制人工制品 [英] ploting artefact with points over raster
问题描述
在调整绘图窗口大小时,我注意到了一些奇怪的行为.考虑
I noticed some weird behavior when resizing the plot window. Consider
library(sp)
library(rgeos)
library(raster)
rst.test <- raster(nrows=300, ncols=300, xmn=-150, xmx=150, ymn=-150, ymx=150, crs="NA")
sap.krog300 <- SpatialPoints(coordinates(matrix(c(0,0), ncol = 2)))
sap.krog300 <- gBuffer(spgeom = sap.krog300, width = 100, quadsegs = 20)
shrunk <- gBuffer(spgeom = sap.krog300, width = -30)
shrunk <- rasterize(x = shrunk, y = rst.test)
shrunk.coords <- xyFromCell(object = rst.test, cell = which(shrunk[] == 1))
plot(shrunk)
points(shrunk.coords, pch = "+")
如果您调整窗口大小,与基础栅格相比,绘制的点会获得不同的范围.如果您调整窗口大小并再次绘制 shrunk
和 shrunk.coords
,结果会很好.谁能解释一下?
If you resize the window, plotted points get different extent compared to the underlying raster. If you resize the window and plot shrunk
and shrunk.coords
again, the plot turns out fine. Can anyone explain this?
推荐答案
如果直接使用 RasterLayer
方法进行绘图,则不会发生调整大小问题.
If you plot directly with the RasterLayer
method for plot the resize problem does not occur.
## gives an error, but still plots
raster:::.imageplot(shrunk)
points(shrunk.coords, pch = ".")
所以它必须是在调用 .imageplot
方法之前原始绘图调用中的东西.
So it must be something in the original plot call before the .imageplot
method is called.
showMethods("plot", classes = "RasterLayer", includeDefs = TRUE)
如果我们直接调用raster:::.plotraster
,它确实会发生,这是调用raster:::.imageplot
的函数:
It does occur if we call raster:::.plotraster
directly, and this is the function that calls raster:::.imageplot
:
raster:::.plotraster(shrunk, col = rev(terrain.colors(255)), maxpixels = 5e+05)
points(shrunk.coords, pch = ".")
它实际上是在轴标签中,而不是图像本身.看到这个,这忠实地绘制了调整大小:
It is actually in the axis labels, not the image itself. See with this, this plots faithfully on resize:
raster:::.imageplot(shrunk)
abline(h = c(-80, 80), v = c(-80, 80))
但是这样做,resize后这些行不再是[-80, 80]:
But do it like this, and the lines are no longer at [-80, 80] after resize:
plot(shrunk)
abline(h = c(-80, 80), v = c(-80, 80))
所以实际上是在栅格之后绘制的点显示不正确:绘图方法保持纵横比固定,因此扩大绘图不会将栅格圆拉伸"为椭圆.但是,它对之后添加的点做了一些事情,因此对 par()
的调用一定不能正确处理(可能在 raster:::.imageplot
中).
So it is actually the points plotted after the raster that are showing incorrectly: the plot method keeps the aspect ratio fixed, so widening the plot doesn't "stretch" out the raster circle to an ellipse. But, it does something to the points that are added afterwards so the calls to par()
must not be handled correctly (probably in raster:::.imageplot
).
查看问题的另一种方式是表明axis() 不知道绘图使用的空间,这与您在overplotting 时看到的问题相同:
Another way of seeing the problem is to show that axis() does not know the space being used by the plot, which is the same problem you see when overplotting:
plot(shrunk)
axis(1, pos = 1)
当您调整 x 轴长度时,两个轴不再同步.
When you resize the x-axis length the two axes are no longer synchronized.
这篇关于用光栅上的点绘制人工制品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!