18
Climate data in R with the raster package Jacob van Etten

Raster package jacob

Embed Size (px)

Citation preview

Page 1: Raster package jacob

Climate data in R with the raster package

Jacob van Etten

Page 2: Raster package jacob

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.

Page 3: Raster package jacob

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.

Page 4: Raster package jacob

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.

Page 5: Raster package jacob

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.

Page 6: Raster package jacob

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

Page 7: Raster package jacob

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.

Page 8: Raster package jacob

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”)

Page 9: Raster package jacob

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.

Page 10: Raster package jacob

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)

Page 11: Raster package jacob

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

Page 12: Raster package jacob

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.

Page 13: Raster package jacob

Interpolation

See if you can work this out for yourself.

Take a look at the first example of

?interpolate

Page 14: Raster package jacob

Sources of data

For an overview of a lot of relevant climate and weather data, visit this website:

http://iridl.ldeo.columbia.edu/

Page 15: Raster package jacob

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

Page 16: Raster package jacob

Worldclim

Precipitation at 10 minute resolution

wc <- getData(“worldclim”, var=“prec”, res=10)

plot(wc)

Page 17: Raster package jacob

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.

Page 18: Raster package jacob