Search code examples
rplotrasterr-raster

How can I fit correctly a raster plot, and change the size of the colorbar?


I'm using the plot function to graph a rasterbrick thata corresponds to the precipitation in South America using R. In order to plot it, I use the function:

plot(prueba,col=rev(col),breaks=brk,zlim=c(0,33),las=2,yaxt="n",xaxt="n")
axis(side=2, at=(seq(min(iy),max(iy),by=(max(iy)-min(iy))/10)), labels=paste(latsc2,"º",sep=""),las=2)
axis(side=1, at=(seq(min(jx),max(jx),by=(max(jx)-min(jx))/10)), labels=paste(lonsc2,"º",sep=""),las=2)

But the map I get as an output is the following:

enter image description here

The first thing I was wondering is if there's any way of getting rid of the white strips on the sides of the plot.

Also, I wanted to know how to resize the colorbar in order to make it easier to read. Everything I've tried to solve this problems has done no difference.

Would really appreciate any insights!

EDIT

The oputput for dput I get is to large to add it inline so I have it in this file.


Solution

  • You have a bit more control (and a larger default legend) with terra (the replacement of raster)

    library(terra)
    f <- system.file("ex/elev.tif", package="terra")
    r <- rast(f)
    plot(r)
    plot(r, plg=list(cex=1.2))
    

    As for the whitespace. As these are maps, the plots have a fixed aspect ratio. You can set the width of the canvas you are plotting on.