Drawing an artifact with dots above the raster

I noticed some strange behavior when resizing the chart 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 = "+") 

If you resize the window, the plotted points will differ from the base raster. If you resize the window and draw shrunk and shrunk.coords again, the graph is perfect. Can anyone explain this?

+6
source share
2 answers

If you draw directly using the RasterLayer method for the graph, there is no problem with resizing.

 ## gives an error, but still plots raster:::.imageplot(shrunk) points(shrunk.coords, pch = ".") 

Thus, it must be something in the original plot call before the .imageplot method is .imageplot .

  showMethods("plot", classes = "RasterLayer", includeDefs = TRUE) 

This happens 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 = ".") 

These are actually labels on the axis, not the image itself. Look at this, this is true for resizing:

  raster:::.imageplot(shrunk) abline(h = c(-80, 80), v = c(-80, 80)) 

But do it like this, and the lines will no longer be at [-80, 80] after resizing:

 plot(shrunk) abline(h = c(-80, 80), v = c(-80, 80)) 

Thus, these are actually the points constructed after the raster is not displayed correctly: the graph method preserves the aspect ratio, therefore the expansion of the graph does not “stretch” the raster circle to an ellipse. But it does something for the points that are added after that, so par() calls should not be handled correctly (perhaps in raster:::.imageplot ).

Another way to see the problem is to show that axis () does not know the space used by the graph, which is the same problem that you see when rewriting:

 plot(shrunk) axis(1, pos = 1) 

When you resize the x axis, the two axes no longer synchronize.

+4
source

Since you have a raster, try replacing plot () with image (). I had the same issue, but that resolved this for me.

-1
source

Source: https://habr.com/ru/post/887925/


All Articles