使用 rasterVis 和 gridExtra 在 R 中组织栅格图
Organizing raster plot in R using rasterVis and gridExtra
我很难使用 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))
我希望它会 return 像这样:
...但是,地图的大小变得混乱,我得到了这个:
关于如何解决这个问题有什么想法吗?
问题是 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)
这三个例子应该给出如下图:
lay1:
lay2:
lay3:
我很难使用 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))
我希望它会 return 像这样:
...但是,地图的大小变得混乱,我得到了这个:
关于如何解决这个问题有什么想法吗?
问题是 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)
这三个例子应该给出如下图:
lay1:
lay2:
lay3: