Date post: | 11-May-2015 |
Category: |
Technology |
Upload: | ccafs-cgiar-program-climate-change-agriculture-and-food-security |
View: | 1,312 times |
Download: | 0 times |
Climate data in R with the raster package
Jacob van Etten
Packages
There are many packages specifically created for R that allow you to do specialized tasks.
One of these packages is raster, created by Robert Hijmans and Jacob van Etten (mainly the former, though).
The raster package allows you to work with geographical grid (raster) data.
Get raster in RStudio
Click on the “Packages” tab in the lower right corner.
Click “Install Packages”.
Type “raster” and click on “Install”.
Leave “Install dependencies” checked. This will also get some other essential packages.
Load the package
With the following command, we load the package into R. Make sure you put this in the first line of your new script.
library(raster)help(package="raster")
The second function gives you an overview of the functions in the package.
The raster() function
The main function to read raster data into R is called (very conveniently) raster.?rasterLet’s make a raster!r1 <- raster()r1As you can see, there are no values in the raster. Next thing to solve.
Adding values
How many values do we need to fill the raster? The function ncell() will tell us.n <- ncell(r1)Let’s make a vector with n random values between 0 and 1 with the function runif().vals<- runif(n)And we add the values to the raster.values(r1) <- vals
Raster graphics
We make a picture of the raster we just made.plot(r1, main=“My first raster map in R”)Now let’s take a look at the different options that plot() gives.?plotClick “Plot a Raster* object”.Also, take a look at the examples and try some if you want.
Real data
Let’s get some real data to play with.http://goo.gl/4488TThis is a raster representing current conditions (a bit over 1 MB).Unzip the file, and put it in a (new) folder.Now make this folder your working directory in R.setwd(“D:/yourfolder”)
Getting raster data into R
Reading this data into R is really easy now.r2 <- raster(“current_bio_1_1.asc”)
What class is this raster?class(r2)
Plot this raster.
Cutting an area of interest
The function extents requires a vector of 4 values: {xmin, xmax, ymin, ymax}. For instance:newExtent <- extent(c(60, 100, 0, 40))Or choose your own area of interest, for instance using Google Earth.Then cut the new extent out of r2 and visualize.r3 <- crop(r2, newExtent)plot(r3)
Raster algebra
It is very convenient to calculate with rasters.Try this and visualize the result.
r4 <- r3 + sqrt(r3)
What happens when you do the following and why?
r5 <- r2 + r3
Some operations
Aggregating cells means the grid becomes coarser. By default the function aggregate() will take the mean of the cells it will aggregate.r6 <- aggregate(r2, fact=2)Now take a look at the examples under ?aggregate and try to understand what happens.
Interpolation
See if you can work this out for yourself.
Take a look at the first example of
?interpolate
Sources of data
For an overview of a lot of relevant climate and weather data, visit this website:
http://iridl.ldeo.columbia.edu/
Moreover...
Worldclim data are global climate data (get it using the raster package, getData function)
NCDC-NOAA – Global Summary of Day, weather data from thousands of stations (weatherData package)
CCAFS data
Worldclim
Precipitation at 10 minute resolution
wc <- getData(“worldclim”, var=“prec”, res=10)
plot(wc)
Global Summary of Day
Available from: ftp://ftp.ncdc.noaa.gov/pub/data/gsod/
These data are massive.
Use the weatherData package to download these data.