简体   繁体   中英

Control raster legend height

I would like to control the height of the colorbar to the extent of the plot window.

For example, if I use:

if (dev.cur() == 1) x11(width=10,height=9)
par(mfrow=c(2,1))

How can I increase the height of the colorbar to be same height as the plots window? eg

plot(r, legend.only=TRUE, legend.width=1.4, legend.shrink=1, 
     col=colorRampPalette(c("darkred", "red3", "orange2", "orange", 
                            "yellow", "lightskyblue","steelblue3", 
                            "royalblue3", "darkblue"))(12),
     breaks=brks, axis.args=list(at=seq(80, 205, by=10), 
                                 labels=seq(80, 205, by=10), cex.axis=0.9),
     legend.args=list(text='Precipitation (mm)', side=4, font=2, 
                      line=2.3, cex=0.8))

Many thanks AZ.

If I understand correctly, you want to plot a full-height legend beside two plots whose layout is defined by par(mfrow=c(2, 1) .

One way to achieve this is to generate the two plots, then set par(new=FALSE) and plot the raster again with legend.only=TRUE .

library(raster)
r <- raster(matrix(runif(100), ncol=10))

# Set layout; ensure appropriate space at right for legend
par(mfrow=c(2, 1), mar=c(2, 3, 1, 3))

# Plot raster
plot(r, legend=FALSE, zlim=c(0, 1), las=1)

# Plot second object
plot(runif(10), runif(10), pch=21, cex=2, las=1, 
     bg=rev(terrain.colors(10)[sample(10, 10, replace=T)]))

# Revert to c(1, 1) layout and adjust legend margins
par(mfrow=c(1, 1), mar=c(2, 0, 1, 0), new=FALSE)

# Plot legend
plot(r, legend.only=TRUE, legend.shrink=1, legend.width=2, zlim=c(0, 1),
     axis.args=list(at=pretty(0:1), labels=pretty(0:1)),
     legend.args=list(text='Whatever', side=4, font=2, line=2.3))

raster_legend

If you are plotting multiple raster objects with matching extents and resolution, you might consider rasterVis::levelplot , which has a RasterStack method:

library(rasterVis)
s <- stack(replicate(2, raster(matrix(runif(100), nc=10))))  

levelplot(s, layout=c(1, 2), names.attr=c('One', 'Two'), 
          at=seq(0, 1, length.out=100), 
          par.strip.text=list(font=2, cex=1.2))
# Plotting titles for vertical colorkeys is a little fiddly...
grid::grid.text('Precipitation (mm)', rot=90, y=unit(0.5, "npc"), 
                x=unit(0.95, "npc"))

raster_legend2

You can suppress panel labels with par.strip.text=list(cex=0) , and specify a colour ramp with col.regions :

levelplot(s, layout=c(1, 2), 
          col.regions=colorRampPalette(c('darkred', 'red3', 'orange2', 'orange', 
                                         'yellow', 'lightskyblue', 'steelblue3', 
                                         'royalblue3', 'darkblue')),
          at=seq(0, 1, length.out=100), par.strip.text=list(cex=0),
          scales=list(alternating=FALSE))
# Also demonstrating how to adjust fontface and size for legend title
grid::grid.text('Precipitation (mm)', y=unit(0.5, "npc"), 
                rot=90, x=unit(0.95, "npc"), gp=gpar(fontsize=14, font=2))

raster_legend3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM