Rstudio 无法从绘图中正确创建 .png

问题描述 投票:0回答:0

我有几个用 Rstudio 从物种出现地图创建的 tif 文件。因为我想将这些文件用作地图上事件的正确图像,所以我必须将我的 tif 文件转换为 png。

我创建了一个脚本,我将所有的 tif 文件加载到 Rstudio 中。然后通过viridis包把tif的色卡改成viridis色卡,然后出图。之后,该图应以 png 格式保存在我的工作目录中。但是,没有创建文件?


library(raster)
library(viridis)

# set working directory to the folder containing the TIF files

# get list of TIF files in the working directory and its subfolders
tif_files <- list.files(path = ".", pattern = "\\.tif$", recursive = TRUE, full.names = TRUE)

# loop over each TIF file
for (file_path in tif_files) {
  # read TIF file
  raster_obj <- raster(file_path)
  
  # get species name from file name
  species_name <- gsub(".tif", "", basename(file_path))
  
  # plot raster
  plot(raster_obj, col = viridis(100))
  
  # save plot as JPG file using the original species name
  png(file = paste0(species_name, ".png"), width = 800, height = 600, res = 450)

  dev.off()
}
plot rstudio png tiff viridis
© www.soinside.com 2019 - 2024. All rights reserved.