2

I want to plot a raster file.

library(colorRamps) 
library(raster) 

r <- raster(nrows=5, ncols=5, vals=1:25)
plot(r, col =  matlab.like(8))

This produces enter image description here

As you can see, the legend has numbers from 5 to 25. These numbers are not so clear so I want to increase the size of them to be clear and make them bold. Thanks

2

1 Answer 1

6

You can pass axis.args and legend.args as arguments to the legend only function call, as for image.plot in the fields package.

For example, to specify tick positions and labels, It will also accept arguments such as legend.width and legend.shrink.

plot(r, col=topo.colors(100), legend=TRUE, axes=TRUE)
r.range <- c(minValue(r), maxValue(r))
plot(r, legend.only=TRUE, col=topo.colors(100),
     legend.width=1, legend.shrink=0.75,
     axis.args=list(at=seq(r.range[1], r.range[2], 25),
                    labels=seq(r.range[1], r.range[2], 25), 
                    cex.axis=0.6),
     legend.args=list(text='Elevation (m)', side=4, font=2, line=2.5, cex=0.8))
3
  • @agstudy -- The examples section of help(plot, package="raster") is where I first found this. Otherwise -- though it's a little bit of work -- you can chase down the source code of the function that actually plots RasterLayer objects.. Dec 23, 2013 at 19:57
  • @JoshO'Brien Thanks. I read the help but without a lot of success. I even go through the code of getMethod(f='plot',signature=signature('Raster','ANY'))(no need to download all the package).... I think I should get all the sources.
    – agstudy
    Dec 23, 2013 at 20:08
  • @agstudy -- With all the raster (and IIRC sp) plotting functions, you have to get pretty quick at following a trail that often leads through several layers of wrapper functions. Here, you go from getMethod("plot", c("Raster", "ANY")) to raster:::.plotraster to raster:::.imageplot before you see axis.args and what is done with it. Takes a while to get used to, but on the other hand it's good to see how these functions actually work! Dec 23, 2013 at 20:50

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.