R:使用通用图例比例分别绘制和保存 rasterbrick 图层

R: Plot and Save rasterbrick layers separately with common legend scale

我有一个多波段 rasterbrick 对象,我正在寻找一种有效的解决方案来以 .jpg 格式分别绘制和保存每个对象。 我不能使用 spplot() 或 levelplot() 因为对象有 100 层。

目前我计划将每一层写成单独的.tiff,并使用Arcgis 进行绘图。 Here 是我正在处理的图层。

您可以编写一个函数来保存 JPEG 图,然后使用 sapply

library(raster)

rand_raster <- function() {
  r <- raster(nrows = 10, ncols = 10)
  r[] <- runif(100)
  r
}

s <- brick(rand_raster(), rand_raster(), rand_raster())
breaks <- seq(from = min(summary(s)["Min.", ]),
              to = max(summary(s)["Max.", ]),
              length.out = 5)
palette <- colorRampPalette(colors = c("blue", "red"))
cols <- palette(5)

raster_plot <- function(x, s) {
  jpeg(filename = paste(names(s[[x]]), ".jpg"))
  plot(s[[x]], breaks = breaks, col = cols)
  dev.off()
}

sapply(1:nlayers(s), function(x) raster_plot(x, s))