1

我很难使用 rasterVis 和 gridExtra 在 R 中一起绘制地图。这就是我的代码的样子(对不起,它肯定不优雅):

require(rasterVis)
require(gridExtra)

p1 <- levelplot(avit_bac12, margin=FALSE, xlab=NULL, ylab=NULL,  par.settings = RdBuTheme, main=NULL, scales=list(draw=FALSE))
p2 <- levelplot(avit_bac17, margin=FALSE, xlab=NULL, ylab=NULL,  par.settings = RdBuTheme, main=NULL, scales=list(draw=FALSE))
p3 <- levelplot(avit_eng, margin=FALSE, xlab=NULL, ylab=NULL,  par.settings = RdBuTheme, main=NULL, scales=list(draw=FALSE))
p4 <- levelplot(avit_sf, margin=FALSE, xlab=NULL, ylab=NULL,  par.settings = RdBuTheme, main=NULL, scales=list(draw=FALSE))
p5 <- levelplot(bac12_bac17, margin=FALSE, xlab=NULL, ylab=NULL,  par.settings = RdBuTheme, main=NULL, scales=list(draw=FALSE))
p6 <- levelplot(bac12_eng, margin=FALSE, xlab=NULL, ylab=NULL,  par.settings = RdBuTheme, main=NULL, scales=list(draw=FALSE))
p7 <- levelplot(bac12_sf, margin=FALSE, xlab=NULL, ylab=NULL,  par.settings = RdBuTheme, main=NULL, scales=list(draw=FALSE))
p8 <- levelplot(bac17_eng, margin=FALSE, xlab=NULL, ylab=NULL,  par.settings = RdBuTheme, main=NULL, scales=list(draw=FALSE))
p9 <- levelplot(bac17_sf, margin=FALSE, xlab=NULL, ylab=NULL,  par.settings = RdBuTheme, main=NULL, scales=list(draw=FALSE))
p10 <- levelplot(eng_sf, margin=FALSE, xlab=NULL, ylab=NULL,  par.settings = RdBuTheme, main=NULL, scales=list(draw=FALSE))

p.avit <- levelplot(avit, margin=FALSE, xlab=NULL, ylab=NULL, par.settings = magmaTheme, main="Avitabile et al. (2016)", scales=list(draw=FALSE))
p.bac12 <- levelplot(bac12, margin=FALSE, xlab=NULL, ylab=NULL, par.settings = magmaTheme, main="Baccini et al. (2012)", scales=list(draw=FALSE))
p.bac17 <- levelplot(bac17, margin=FALSE, xlab=NULL, ylab=NULL, par.settings = magmaTheme, main="Baccini et al. (2017)", scales=list(draw=FALSE))
p.eng <- levelplot(eng, margin=FALSE, xlab=NULL, ylab=NULL, par.settings = magmaTheme, main="Englund et al. (2017)", scales=list(draw=FALSE))
p.sf <- levelplot(sf, margin=FALSE, xlab=NULL, ylab=NULL, par.settings = magmaTheme, main="Soares-Filho et al. (2017)", scales=list(draw=FALSE))

p.list <- list(p.avit, p.bac12, p.bac17, p.eng, p.sf,
               p1, p2, p3, p4, p5, p6, p7, p8, p9, p10)

lay <- rbind(c(NA,  2,  3,  4,  5),
             c(1,   6,  7,  8,  9),
             c(2,   NA, 10, 11, 12),
             c(3,   NA, NA, 13, 14),
             c(4,   NA, NA, NA, 15))

grid.arrange(grobs=p.list, layout_matrix=lay,
         vp = grid::viewport(width=0.4,height=0.3))

我希望它会返回如下内容:在此处输入图像描述

...但是相反,地图的大小搞砸了,我得到了这个:在此处输入图像描述

关于如何解决这个问题的任何想法?

4

1 回答 1

1

问题是其中的一些数字layout_matrix是重复的。重复数字意味着绘图应覆盖网格中具有相同数字的单元格。由于您的每个图都应占用网格中的一个单元格,因此布局矩阵中的数字不应重复。如果需要将绘图放置在两个位置,请将其传递给grid.arrange两次,但请确保layout_matrix包含唯一的数字(NA如果需要,还需要 s)。使用示例更容易:

library(ggplot2)
library(gridExtra)
p <- ggplot(iris, aes(x=Sepal.Width, y=Sepal.Length, color=Species)) + geom_point(show.legend = FALSE)

lay1 <- rbind(c(NA, 1,2), 
              c(2,3, NA))
grid.arrange(p,p,p, layout_matrix=lay1) 
# adding a 4th plot in the above grid.arrange call results with error

lay2 <- rbind(c(NA, 1,2), 
              c(3,4, NA))
grid.arrange(p,p,p,p, layout_matrix=lay2)

lay3 <- rbind(c(1,1,NA,2),
              c(1,1,3, NA))
grid.arrange(p,p,p, layout_matrix=lay3)

这三个示例应给出以下图:

铺设1:

在此处输入图像描述

层2:

在此处输入图像描述

层3:

在此处输入图像描述

于 2019-08-22T04:30:00.650 回答