matandked
matandked

Reputation: 1563

R - the simplest way to move legend when plotting raster

I need to plot raster files.

In my output image, numbers on the legend are sometimes not visible - especially when I split graph window to two or more columns (for example: par(mfrow=c(1,2))).

I thought about moving legend to the bottom (below raster image) to resolve this issue. However, most examples which I found suggest creating completely new legend with completely new colors and items definitions.

I would like to use the default legend. I just need to move it to the bottom. I've tried to do this as follows:

library('raster')
data(volcano)
r <- raster(volcano)
# Trying to draw default legend below raster plot
plot(r, legend=F)
# Now trying to draw legend. Default is okay for me, I want to move it below only:
plot(r, legend.only=TRUE, legend.args=list("bottom", text='My values [m^3]'))

Unfortunately, my code is not working (seems "bottom" parameter is not used).

What I need to achieve

Upvotes: 4

Views: 7733

Answers (3)

mikoontz
mikoontz

Reputation: 592

tl;dr

Try passing horizontal = TRUE to the plot() function call.

library('raster')

data(volcano)
r <- raster(volcano)

plot(r, legend.only=TRUE, horizontal = TRUE, legend.args = list(text='My values [m^3]'))

enter image description here

Tweaks

You can now pass side= or line= arguments to the legend.args list to specify where you want the legend label (default is side = 3 and line = 0). For instance, you could put the "My values [m^3]" text below the legend by using side = 1 and space it a bit further from the legend (versus on top of it!) using line = 2.

plot(r, legend.only=TRUE, horizontal = TRUE, 
        legend.args = list(text='My values [m^3]', side = 1, line = 2))

Advanced tweaks

If you want to plot the legend somewhere other than the right or bottom of the plot, you'll have to look into first plotting your raster with axes = FALSE and then plotting it again but passing as arguments legend.only = TRUE and smallplot= c(xleft, xright, ybottom, ytop) to specify where in the plotting region to draw the colored box.

Background

The trick is that the legend.args list gets passed to the mtext() function, so the standard way of defining legend locations in base R plotting with the legend() function and using the x= argument (e.g., "bottom", "bottomright") isn't available.

You might think that the axis.args list would help, since that also (counterintuitively perhaps) controls how the legend is drawn. The axis.args list is passed to the axis() function to draw some features of the legend, and the axis() function has a side= argument that sets which side of the plot the axis (in our case the legend) will be drawn! But no, the side= argument is set by other means during the drawing of the raster plot.

What means are those you might ask? It's the horizontal= argument!

The horizontal= argument can be passed to the plot() call to say whether you want the raster legend to be on the right of the plot (the default, horizontal = FALSE sets the side= argument to 4 in the axis() function call that draws the legend) or on the bottom of the plot (when horizontal = TRUE which sets the side= argument to 1 in the `axis() function call that draws the legend)

Upvotes: 6

Edzer Pebesma
Edzer Pebesma

Reputation: 4121

Or try spplot, which also uses package lattice:

spplot(r, scales = list(draw = TRUE), colorkey = list(space = "bottom"))

Upvotes: 0

johannes
johannes

Reputation: 14453

Here is a way using rasterVis:

library(rasterVis)
levelplot(r, margin=FALSE, colorkey=list(space="bottom"), par.settings = RdBuTheme())

enter image description here

Upvotes: 0

Related Questions