【问题标题】:rasterVis - setting the bottom plots in the middle with levelplotrasterVis - 使用 levelplot 在中间设置底部图
【发布时间】:2020-10-25 02:03:42
【问题描述】:

我正在使用很棒的 rasterVis 创建一个面板,其中包含具有相同范围(即相同空间覆盖)但显示不同特征(即每个都有自己的图例)的地图。

这是目前的样子:

library(raster)
library(rasterVis)
library(RColorBrewer)
library(gridExtra)

# make-up data
r <- raster(system.file("external/test.grd", package="raster"))
s <- stack(r, r*2, r*3, r*4, r*5)
names(s) <- paste0("Field ",seq(1,5))

# pre-allocate list
l <- vector("list", length=nlayers(s))

# define theme for plots
my.theme <- rasterTheme(region=brewer.pal(11,'RdYlGn'))

# loop over stack layers to fill list
for (n in (1:nlayers(s))){
  
  l[[n]] <- levelplot(s[[n]], margin=F, main=names(s[[n]]), par.settings=my.theme)
  
}

# plot combined maps
grid.arrange(l[[1]], l[[2]], l[[3]], l[[4]], l[[5]], ncol=3)

请注意,地图的默认定位是:

a b c
d e

但是,我希望对定位进行更精细的控制。具体来说,我想将底部的两个面“居中”,以便在绘图的两侧更均匀地分布空隙空间。

换句话说,我正在寻找的展示位置如下所示:

a b c
 d e

我怎样才能做到这一点?我查找了 (grid.arrange) 的文档,但找不到任何可以解决我的问题的选项。

提前感谢任何提示。

【问题讨论】:

    标签: r lattice gridextra levelplot rastervis


    【解决方案1】:

    这是一种受this answer启发的cowplot::plot_grid方法

    library(cowplot)
    top <- plot_grid(l[[1]], l[[2]], l[[3]],
                     ncol=3)
    bottom <- plot_grid(NULL, l[[4]], NULL,  l[[5]], NULL,
                        ncol=6, rel_widths=c(0.12,0.34,0.12,0.34,0.06))
    plot_grid(top, bottom,
              ncol=1, rel_heights=c(1,1))
    

    您可以修改底部plot_grid 调用中的rel_widths 以获得您喜欢的。

    【讨论】:

    • 效果很好,谢谢!您指出的答案也有一个非常漂亮的解决方案,使用gridExtra
    【解决方案2】:

    您可以提供一个包含 6 列的布局,并使用 NA 作为空白,

    library(gridExtra)
    
    p <- ggplot()
    
    grid.arrange(grobs = list(p,p,p,p,p), 
                 layout_matrix = rbind(c(1,1,2,2,3,3), c(NA,4,4,5,5,NA)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-06
      • 2021-06-23
      • 1970-01-01
      • 1970-01-01
      • 2018-09-22
      • 2016-10-23
      相关资源
      最近更新 更多