R scripts
R-scripts to deal with netCDF files, perform downscaling and compute 19 bioclimatic variables are available at github.com/ecoClimate.
Explore them and advance your limits on new variables.
Here you have an R-script for opening ecoClimate.org layers in R. Our climatic layers are stored as *.txt* files. Thus, you need to open them using the function read.table and then construct raster files (maps) as follows:
1. download the climatic layers (.txt files) from ecoClimate.org
2. save them in a folder in your computer and use the function ‘setwd’ to set this folder as the working directory
3. open the file in R using ‘read.table’
setwd("Path/Of/Folder/With/Climatic/Layers") CCSM_LGM <- read.table("FileName.txt", header = TRUE) head(CCSM_LGM)
4. load the libraries to work with GIS formats in R, (install them first if you do not have them in your computer; use the function ‘install.packages’)
library(sp) library(raster)
5. Run this simple code in order to construct raster files for each climatic layer
gridded(CCSM_LGM) <- ~long+lat map_CCSM_LGM <- stack(CCSM_LGM)
6. Finally, you can plot the raster files using your own palette of colours
colores_t <- colorRampPalette (c("darkblue", "blue", "lightblue", "white", "salmon", "red"))
in this example, we are plotting the temperature variables (thus, we select the maps 2 to 12))
plot (map_CCSM_LGM [[c(2:12)]], axes=F, box=F, col=colores_t (100))
here, we use $ in order to select one specific variable (e.g., bio.1)
par (mar=c(1,1,1,1)) plot (map_CCSM_LGM$bio.1, axes=F, box=F, col=colores_t(100), legend=T) title ("LGM Bio1 CCSM")
now, we are going to change the colours because we want to plot the precipitation layers (13 to 20)
colores_p <- colorRampPalette (c("white","lightblue","blue", "darkblue")) plot (map_CCSM_LGM [[c(13:20)]], axes=F, box=F, col=colores_p (100))
we hope that this R-script will help you to understand how to easily work in R with raster files
Best,
Sara Varela and Matheus Lima-Ribeiro