简体   繁体   English

控制栅格图例高度

[英]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. 非常感谢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) . 如果我理解正确,那么您想在两个布局由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 . 一种实现方法是生成两个图,然后设置par(new=FALSE)并使用legend.only=TRUE再次绘制raster

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: 如果要绘制范围和分辨率匹配的多个raster对象,则可以考虑rasterVis::levelplot具有RasterStack方法的rasterVis::levelplot

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 : 您可以使用par.strip.text=list(cex=0)取消显示面板标签,并使用col.regions指定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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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