0

I am trying to plot this raster on R. The object is quite big, but when plotting it using the raster::plot() function, I obtain a very tiny image, as attached.

The code I use to produce it is the following:

r <- raster::raster("MOD13Q1_2000-02-18.250m_16_days_NDVI.tif")
r <- setMinMax(r)
raster::plot(r)

I tried to set xlim limits, with no success. I then tried to move to ggplot2, as an alternative (following here), but I obtain the following

ggplot(r) + geom_tile(aes(fill=value))
Error: ggplot2 doesn't know how to deal with data of class RasterLayer

I found around that, to plot raster objects with ggplot(), I should first convert it into a data.frame. However, as my object is quite big (about 55million obs.), that would be very inefficient. I am then stuck on what to do. Any thoughts?

enter image description here

2

The base plot method works fine for me:

enter image description here

so I suspect something has gone amiss with your graphics device. If using rstudio, I think there's a little button to reset the graphics window, otherwise try dev.off() to switch the graphics window off, and the next plot will create a new one.

This behaviour generally happens when a previous graphics operation has left the graphics device in a non-default state, such as having divided it up to put a legend in a section of window. A reset as described above usually helps.

There's no need to look to ggplot - the raster package plot methods are very well optimised for big rasters and will sample down large rasters for speed.

  • Thanks for you answer. For some reason it still does not work, and clearly there is a problem with my graphic device. I tried other rasters from tutorials on the web and I am having the same issue. My issue is not with the picture, then, but with some setup on my plotting device. Any thoughts on how to fix that? – Bruno Conte Leite Dec 13 '17 at 13:21
  • Edit your question and give some more info on your system, including operating system, R version, raster version etc. If you restart R and do plot(raster::raster(matrix(1:100,10,10))) does it still fail? – Spacedman Dec 13 '17 at 13:23
1

In addition to @Spacedman answer... Sometimes, some packages change plot parameters.

You need to set back default parameters and plot again:

r <- raster::raster("MOD13Q1_2000-02-18.250m_16_days_NDVI.tif")

par(mar = c(5.1, 4.1, 4.1, 2.1), mgp = c(3, 1, 0), mfrow = c(1,1), las = 0)

plot(r)

You can also include this into plot function to override an unexpect behaivour:

r <- raster::raster("MOD13Q1_2000-02-18.250m_16_days_NDVI.tif")

plot(r,mar = c(5.1, 4.1, 4.1, 2.1), mgp = c(3, 1, 0), mfrow = c(1,1), las = 0)
  • Thanks for your message. It still does not work. I am facing this problem with other tif files, as well. Any suggestions? – Bruno Conte Leite Dec 13 '17 at 13:26
  • Are you using RStudio? – aldo_tapia Dec 13 '17 at 15:38
  • Yes I am, and indeed I guess that is the problem. I manage to plot it on R, or even running R through the Terminal (I am using a mac). I guess I will open a new question as this one has somehow lost sense... – Bruno Conte Leite Dec 14 '17 at 9:03
  • I also work with RStudio on macOS. Maybe plot window is too small, try resizing borders – aldo_tapia Dec 14 '17 at 9:11
  • Resizing borders did not work at the end. The issue was some changes made by the rts package I installed - I have already contacted its developer. Interestingly, plotting with rastervis::levelplot() works fine. – Bruno Conte Leite Mar 20 '18 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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