1

I have a hillshade in the form of a SpatRaster object. When I plot it like this:

plot(hs.rast, col = grey(c(0:100)/100), legend = F),

the plotting function seems to apply some sort of smoothing to the image, as in image(), when the useRaster = TRUE argument is used. In the documentation of image(), this is called plotting using a bitmap raster. The same thing seems to be applied here. This makes the hillshade plot appear much too grainy. This occurs both when plotting to an RStudio plotting device and when plotting to .pdf.

Is there a way how I can plot the SpatRaster without bitmap plotting, i.e. plot every single raster cell? This should result in a much sharper image.

Edit 11. October 2022:

The hillshade is based on regridded DEM data from JAXA ALOS, over the area of Switzerland. It has horizontal resolution of 100m in easting and northing (Swiss LV95 coordinate system) and dim(hs.rast) is 2650 3700 1. Now,

plot(hs.rast, col = grey(c(0:100)/100), legend = F)

yields

enter image description here

which is a selection of the domain. (Attention: don't zoom in the picture, since it is a .png screenshot of what I plotted to .pdf, meaning you will see crisp cells due to the .png. Apparently SO does not permit upload of .pdf's). This is the original problem, with the image being much too grainy.

When I do:

plot(hs.rast, col = grey(c(0:100)/100), legend = F, maxcell = Inf), I get:

enter image description here

which is much better. However, when I zoom in, I see:

enter image description here

which is still a smoothed surface, not crisp cells. When I now do

plot(hs.rast, col = grey(c(0:100)/100), legend = F, maxcell = Inf, smooth = FALSE), I get:

enter image description here

on zooming in. Still a smoothed surface, not crisp cells. Apparently, the smooth = FALSE argument did not change much.

I know that the problem is largely solved by using the maxcell = Inf argument, but still it would be nice to have a totally crisp image, especially for data art purposes (large printouts). Any further input is welcome.

3
  • If I create a 3x4 matrix and turn it into a SpatRaster and plot that I see a crisp 3x4 grid of cells. No smoothing. Can you post screenshots of what you see and details of the size, extent, projection etc of your raster (or share your data?)?
    – Spacedman
    Commented Oct 5, 2022 at 16:43
  • Thanks for the comment. I added more data set details and especially plots to illustrate the point.
    – Abdirizak
    Commented Oct 11, 2022 at 9:47
  • As for sharing the data, I don't think the JAXA license allows me to do that. But take any large, highly resolved DEM, calculate the hillshade, plot to pdf, and you will see.
    – Abdirizak
    Commented Oct 12, 2022 at 9:55

2 Answers 2

5

Without your data is hard to tell, but be aware that terra::plot() has a parameter maxcell=500000 that resamples the raster if the number of cells is greater than that value.

Maybe try


plot(hs.rast, col = grey(c(0:100)/100), legend = F, 
  maxcell = terra::ncell(hs.rast)*1.1)

3
  • Thanks for the hint. This makes the plot look much better and sharper. However, when zooming in, I still see a smoothed surface, not crisp cells. Any ideas?
    – Abdirizak
    Commented Oct 6, 2022 at 12:45
  • 1
    You can use arguments maxcell=Inf and smooth=FALSE Commented Oct 6, 2022 at 22:16
  • Thanks for the advice. However, smooth = FALSE does not seem to change much (see my edit of the original question).
    – Abdirizak
    Commented Oct 11, 2022 at 9:47
0

Ok, I finally found the answer. The problem is with my PDF viewer (Preview.app, which does some smoothing), not with the PDF. When I open my PDF on here and zoom in, I see:

enter image description here

Which is the desired crisp image.

Apparently MacOS offers some options to enable / disable image smoothing in the system preferences (not in the Preview settings), but I refrain from giving details here since that is better documented elsewhere and also will change over time.

Credits to eipi10 for pointing out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.