Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Climate data in r with the raster package


Published on

Published in: Technology, Business
  • Be the first to comment

Climate data in r with the raster package

  1. 1. Climate data in Rwith the raster package Jacob van Etten Alberto Labarga
  2. 2. PackagesThere are many packages specifically createdfor R that allow you to do specialized tasks.One of these packages is raster, created byRobert Hijmans and Jacob van Etten (mainlythe former, though).The raster package allows you to work withgeographical grid (raster) data.
  3. 3. Get raster in RStudioClick on the “Packages” tab in the lower rightcorner.Click “Install Packages”.Type “raster” and click on “Install”.Leave “Install dependencies” checked. This willalso get some other essential packages.
  4. 4. Load the packageWith the following command, we load thepackage into R. Make sure you put this in the firstline of your new script.library(raster)help(package="raster")The second function gives you an overview of thefunctions in the package.
  5. 5. The raster() functionThe main function to read raster data into R iscalled (very conveniently) raster.?rasterLet’s make a raster!r1 <- raster()r1As you can see, there are no values in theraster. Next thing to solve.
  6. 6. Adding valuesHow many values do we need to fill theraster? The function ncell() will tell us.n <- ncell(r1)Let’s make a vector with n random valuesbetween 0 and 1 with the function runif().vals<- runif(n)And we add the values to the raster.values(r1) <- vals
  7. 7. Raster graphicsWe 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 optionsthat plot() gives.?plotClick “Plot a Raster* object”.Also, take a look at the examples and try someif you want.
  8. 8. Real dataLet’s get some real data to play with. 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 directoryin R.setwd(“D:/yourfolder”)
  9. 9. Getting raster data into RReading 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.
  10. 10. Cutting an area of interestThe 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 instanceusing Google Earth.Then cut the new extent out of r2 and visualize.r3 <- crop(r2, newExtent)plot(r3)
  11. 11. Raster algebraIt is very convenient to calculate with rasters.Try this and visualize the result.r4 <- r3 + sqrt(r3)What happens when you do the following andwhy?r5 <- r2 + r3
  12. 12. Some operationsAggregating cells means the grid becomescoarser. By default the function aggregate()will take the mean of the cells it willaggregate.r6 <- aggregate(r2, fact=2)Now take a look at the examples under?aggregate and try to understand whathappens.
  13. 13. InterpolationSee if you can work this out for yourself.Take a look at the first example of?interpolate
  14. 14. Sources of dataFor an overview of a lot of relevant climateand weather data, visit this website:
  15. 15. Moreover...Worldclim data are global climate data (get itusing the raster package, getData function)NCDC-NOAA – Global Summary of Day,weather data from thousands of stations(weatherData package)CCAFS data
  16. 16. WorldclimPrecipitation at 10 minute resolutionwc <- getData(“worldclim”, var=“prec”, res=10)plot(wc)
  17. 17. Global Summary of DayAvailable from: data are massive.Use the weatherData package to downloadthese data.