3

I am currently trying to create a multi-panel plot to display six raster images (3,2) using par() and plot(raster()). When I plot the rasters though, R is displaying the Y bounds far beyond what is necessary, resulting in lots of wasted space. How can I adjust this? When, I run extent() on the raster images themselves, the extent is correct.

a<-raster("file.tif")
...and so on to f.

par(mfrow=c(2,3))
plot(a)
plot(b)
plot(c)
plot(d)
plot(e)
plot(f)

Example of a single map output

2
  • Constrain the aspect ratio with asp=1? `plot(a,asp=1)...
    – Dave X
    Commented Nov 18, 2020 at 22:23
  • 2
    Manually changing the aspect is not a good idea. That would distort the map. Commented Nov 18, 2020 at 22:30

5 Answers 5

1

Possibly a duplicate of this post. For some reason setting

plot(a,ext=extent(a),xaxs="i",yaxs="i")

does not work properly. xaxs,yaxs are internal variables in R that set a margin on data link. The documentation of the raster library says the above method should work. Try:

spplot(a)

that worked for me

1

You control the size of the plot area, plot(raster) just follows your lead. So set it to something you like --- that makes sense given the dimensions of the plots you are making. How you do that depends what "device" you plotting on and whether you use Rstudio or not. See e.g. ?png or dev.new(width=5, height=4). Or, in Rgui, resize manually and then plot again.

0
  1. As Gevaert mentioned, I think this has been asked before, but I'll respond to this post since it's fairly recent.

  2. I think Robert above made a good point about how the plot will stretch to the window.

Still, I found that raster::plot's documentation indicates that you can specify the plotting window and in my experience it has zero effect. Instead it will always assume the extent of the raster you give it.

My suggested workaround is to first draw your desired plot window with blank object then add the raster plot to this window.

my_window <- extent(-83,-82.85,28,28.2)
plot(my_window, col=NA)
plot(my_raster, add=T)

For me, this achieves what I'm after. The caveat is if you're plotting a stack or brick, the add functionality doesn't work in my experience. The solution is to use subset, like plot(subset(my_brick,4), add=T)

2
  • You say you found "that you can specify the plotting window and in my experience it has zero effect.". Can you show that? It clearly works when I do it. Commented Dec 3, 2020 at 19:50
  • Just tried to reproduce this and now it works! In the past, I've spent a good deal of time trying to debug this so I know that it fails sometimes. I often am plotting rasters over spatialPolygons, using various plotting calls (raster::plot and sp::plot) so it's possible that I've just been doing something dumb. I'll update if I can determine a specific case and find out more info. Commented Dec 28, 2020 at 13:30
0

For me the following works:

extent(a) <- c(-180, 180, -180, 180)
crs(a) <- NULL
plot(a)

I do crs(a) <- NULL to avoid a warning.

0
  box=extent(my_raster)
  yxscale=(box@ymax-box@ymin)/(box@xmax-box@xmin)
  xsize=3 # in inches
  ysize=xsize*yxscale
  par(pin=c(xsize,ysize)) # to have a plot extenting to the limits of 
                            the raster
  plot(my_raster)

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.