2

If I use plot() to draw a GIS raster image, then use points() to add some points on the image: E.g. the following code

in_rast_str <- "PET_eclp.tif"
in_rast <- raster(in_rast_str)
selected_cells <- choose_points(in_rast_str,10,30)
plot(in_rast)
points(selected_cells[,1],selected_cells[,2])

The initial output renders correctly. However, if I then resize the plot either in the export window, or in the zoom window of RStudio, the points and the underlying raster shift relative to each other and become misaligned.

Is this a problem with RStudio or with R? I'm guessing that this could be a bug rather than that I am doing something obviously wrong.

Update...

Here are some examples: original plot

and the same plot, but resized

enter image description here

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
dww
  • 30,425
  • 5
  • 68
  • 111
  • I wonder if aspect ratio has anything to do with this? – Roman Luštrik Feb 17 '15 at 13:15
  • 1
    Thanks Roman. I think you are on the right tracks that it is related to aspect ratio. But I don't think that is the whole story. I think it is something to do with the raster being rescaled, but the points being plotted still at the original scale. In the examples pictures I added to the post, you can see that not only the positions of the points change but also their size. So it looks like a scaling issue rather than just an aspect ratio one. – dww Feb 17 '15 at 14:15
  • 1
    I thought there's something awfully familiar about this. I asked the same question a while ago: http://stackoverflow.com/questions/5977419/ploting-artefact-with-points-over-raster – Roman Luštrik Feb 17 '15 at 14:24

1 Answers1

2

That issue with raster has been annoying me for years.

This doesn't exactly answer your question, but rasterVis provides a very pleasing (to me, anyway) workaround.

library(rasterVis)
r <- raster(matrix(runif(100), 10))  
xy <- xyFromCell(r, which(values(r) > 0.9))

levelplot(r, margin=FALSE) + layer(sp.points(xy, pch=20, cex=2, col=1))

enter image description here

Resize and zoom around as much as you like - the points will stick to the correct cells.

jbaums
  • 27,115
  • 5
  • 79
  • 119
  • 3
    @mdsumner has drilled down the problem and it's points() that's causing hot blood. http://stackoverflow.com/a/5985523/322912 – Roman Luštrik Feb 17 '15 at 14:27
  • @Roman - thanks, just saw the post you linked. Good to know! (But I'll stick with `rasterVis` in most cases!) – jbaums Feb 17 '15 at 14:28