R code and datasets to support "Stability and changes of global climate diversity by 2100"
收藏资源简介:
1. System requirements Software dependencies - R (version 4.3.3 or 4.4.2) - RStudio (2026.04.0) - R packages (install once with `install.packages(c("raster", "stringr", "ggplot2", "reshape2", "dplyr", "patchwork"))`) Operating system - Windows 10 (tested) Tested versions R 4.3.3 on Windows 10 with RStudio 2026.04.0 Non‑standard hardware None. A standard desktop or laptop computer is sufficient. 2. Installation guide No installation required This code runs directly in R / RStudio. No separate software installation is needed beyond R and the required packages. Steps to prepare 1. Download and unzip the provided `.rar` archive. 2. Open the project in RStudio. 3. Set the working directory to the unzipped folder. 4. Install missing R packages if prompted:install.packages(c("raster", "stringr", "ggplot2", "reshape2", "dplyr", "patchwork")) Installing these packages typically takes about 10 minutes. Repository structure and important notes Here we provide the core R code to calculate climate diversity. To enable fast computation and obtain results, we selected five small regions globally (same as the five regions shown in Extended Data Fig. 10 of the manuscript) and computed the spatial patterns of climate diversity under seven different moving window sizes for the current period. After you download and unzip the .rar file, three subfolders will be extracted: Folder: "code"All R code is stored in the code folder, which contains four R script files. The scripts are designed to be run in order: step1 → step2 → step3 (set paths first) → step4. See Section 3 (Demo) for detailed run instructions. Folder: "data" The PCA factors for the five regions at a 2.5‑arcminute resolution are saved in the subfolder pca_scl. Each region’s subfolder contains one .tif file, which is a stacked raster of four selected PCA factors extracted from the global standard PCA factors. Climate diversity results for each of the seven moving window sizes are stored in the disc_merge subfolder under each region’s directory. The disc_zones_txt folder retains intermediate processing files generated during the calculation. Important notes (1) Direct use of precomputed resultsWe have already computed climate diversity results for each region under different moving window sizes. These are stored in the corresponding directories. For example:...\CodeAndData\data\andes\disc_merge\disc_cur_mw5.tifThis file represents the climate diversity result for the Andes region with a 5×5 moving window. You can directly use these precomputed results to run the step4 scripts and generate the plots. (2) Recomputing climate diversityIf you wish to recalculate the climate diversity distribution for a specific region and moving window size on your own computer: Manually delete all files in the disc_merge and disc_zones_txt subfolders under the target region’s directory. Re‑run step3-MAIN-cd-cur.R. 3. Demo We provide a ready‑to‑run demo using five small regions (Andes, etc.) and seven moving window sizes, as shown in Extended Data Fig. 10. Instructions to run on data (reproducing Extended Data Fig. 10 current‑period panels) Option A – Using pre‑computed results (quickest)Skip step 3 below, directly use the existing files in .../disc_merge/ and only run step4-subfig-cur.R to see the figures immediately. Option B – Full recomputation Open step1-function_cd.R and step2-function_combine_zones.R – run both to load functions. Manually delete all files in the disc_merge and disc_zones_txt subfolders under the target region’s directory. Open step3-MAIN-cd-cur.R. Before running, change the file paths to match your local directory (e.g., setwd("D:/YourPath/CodeAndData")). Run step3-MAIN-cd-cur.R to compute climate diversity. Open step4-subfig-cur.R and run it to generate the plots. Expected output For each region and moving window size: a GeoTIFF file (disc_cur_mw*.tif) in the disc_merge folder. Figures: the current‑period panels exactly as shown in Extended Data Fig. 10 of the manuscript (saved as .jpg in the output folder). Expected run time on a normal desktop computer Using pre‑computed results (step 4 only): < 1 minute Full recomputation (steps 3 + 4): approximately 10 minutes on a standard Windows desktop (Intel i7, 32GB RAM). 4. Instructions for use – How to run the software on your own data If you want to apply the same climate diversity calculation to your own study regions or global data: Prepare your input data Your environmental layers (e.g., temperature, precipitation) must be in GeoTIFF format at the desired spatial resolution. We recommend first extracting PCA factors (following the method in the manuscript) from your own data. Modify the configuration In step3-MAIN-cd-cur.R, change the input file paths to point to your own PCA factor rasters. Adjust the moving window sizes (e.g., window_sizes = c(3,5,7,9,11,13,15)) as needed. Update the region names and corresponding directories. Run the pipeline Source step1-function_cd.R and step2-function_combine_zones.R. Run step3-MAIN-cd-cur.R to compute climate diversity for your data. Use step4-subfig-cur.R (or your own plotting script) to visualise the results. Expected outputs GeoTIFF maps of climate diversity for each moving window size. You can compare diversity patterns across window sizes or time periods (e.g., future scenarios by modifying the input data). For any issues, please check that all required R packages are installed and that file paths use forward slashes (/) or double backslashes (\\) in R.



