Climate data in R with the raster package

4,407 views

Published on

Presentation by Jacob van Etten.

CCAFS workshop titled "Using Climate Scenarios and Analogues for Designing Adaptation Strategies in Agriculture," 19-23 September in Kathmandu, Nepal.

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,407
On SlideShare
0
From Embeds
0
Number of Embeds
114
Actions
Shares
0
Downloads
81
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Climate data in R with the raster package

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

×