Climate data in r with the raster package
-
Upload
alberto-labarga -
Category
Technology
-
view
2.730 -
download
1
Transcript of Climate data in r with the raster package
![Page 1: Climate data in r with the raster package](https://reader031.fdocuments.net/reader031/viewer/2022020217/554d9881b4c905525e8b496a/html5/thumbnails/1.jpg)
Climate data in R with the raster package
Jacob van Etten Alberto Labarga
![Page 2: Climate data in r with the raster package](https://reader031.fdocuments.net/reader031/viewer/2022020217/554d9881b4c905525e8b496a/html5/thumbnails/2.jpg)
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: Climate data in r with the raster package](https://reader031.fdocuments.net/reader031/viewer/2022020217/554d9881b4c905525e8b496a/html5/thumbnails/3.jpg)
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: Climate data in r with the raster package](https://reader031.fdocuments.net/reader031/viewer/2022020217/554d9881b4c905525e8b496a/html5/thumbnails/4.jpg)
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: Climate data in r with the raster package](https://reader031.fdocuments.net/reader031/viewer/2022020217/554d9881b4c905525e8b496a/html5/thumbnails/5.jpg)
The raster() function
The main function to read raster data into R is called (very conveniently) raster. ?raster Let’s make a raster! r1 <- raster() r1 As you can see, there are no values in the raster. Next thing to solve.
![Page 6: Climate data in r with the raster package](https://reader031.fdocuments.net/reader031/viewer/2022020217/554d9881b4c905525e8b496a/html5/thumbnails/6.jpg)
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: Climate data in r with the raster package](https://reader031.fdocuments.net/reader031/viewer/2022020217/554d9881b4c905525e8b496a/html5/thumbnails/7.jpg)
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. ?plot Click “Plot a Raster* object”. Also, take a look at the examples and try some if you want.
![Page 8: Climate data in r with the raster package](https://reader031.fdocuments.net/reader031/viewer/2022020217/554d9881b4c905525e8b496a/html5/thumbnails/8.jpg)
Real data
Let’s get some real data to play with. http://goo.gl/4488T This 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: Climate data in r with the raster package](https://reader031.fdocuments.net/reader031/viewer/2022020217/554d9881b4c905525e8b496a/html5/thumbnails/9.jpg)
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: Climate data in r with the raster package](https://reader031.fdocuments.net/reader031/viewer/2022020217/554d9881b4c905525e8b496a/html5/thumbnails/10.jpg)
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: Climate data in r with the raster package](https://reader031.fdocuments.net/reader031/viewer/2022020217/554d9881b4c905525e8b496a/html5/thumbnails/11.jpg)
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: Climate data in r with the raster package](https://reader031.fdocuments.net/reader031/viewer/2022020217/554d9881b4c905525e8b496a/html5/thumbnails/12.jpg)
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: Climate data in r with the raster package](https://reader031.fdocuments.net/reader031/viewer/2022020217/554d9881b4c905525e8b496a/html5/thumbnails/13.jpg)
Interpolation
See if you can work this out for yourself. Take a look at the first example of ?interpolate
![Page 14: Climate data in r with the raster package](https://reader031.fdocuments.net/reader031/viewer/2022020217/554d9881b4c905525e8b496a/html5/thumbnails/14.jpg)
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: Climate data in r with the raster package](https://reader031.fdocuments.net/reader031/viewer/2022020217/554d9881b4c905525e8b496a/html5/thumbnails/15.jpg)
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: Climate data in r with the raster package](https://reader031.fdocuments.net/reader031/viewer/2022020217/554d9881b4c905525e8b496a/html5/thumbnails/16.jpg)
Worldclim
Precipitation at 10 minute resolution wc <- getData(“worldclim”, var=“prec”, res=10) plot(wc)
![Page 17: Climate data in r with the raster package](https://reader031.fdocuments.net/reader031/viewer/2022020217/554d9881b4c905525e8b496a/html5/thumbnails/17.jpg)
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: Climate data in r with the raster package](https://reader031.fdocuments.net/reader031/viewer/2022020217/554d9881b4c905525e8b496a/html5/thumbnails/18.jpg)