SlideShare a Scribd company logo
Climate data in R with the raster packageJacob van Etten
PackagesThere 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 RStudioClick 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 packageWith 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")Thesecondfunctiongivesyouanoverview of thefunctions in thepackage.
The raster() functionThe 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 valuesHow 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 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 options that plot() gives.?plotClick “Plot a Raster* object”.Also, take a look at the examples and try some if you want.
Real dataLet’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 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.
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))Orchooseyourownarea of interest, forinstanceusing Google Earth.Then cut the new extent out of r2 and visualize.r3 <- crop(r2, newExtent)plot(r3)
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 and why?r5 <- r2 + r3
Some operationsAggregating 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.
InterpolationSee if you can work this out for yourself.Take a look at the first example of ?interpolate
Sources of dataFor 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
WorldclimPrecipitation at 10 minute resolutionwc <- getData(“worldclim”, var=“prec”, res=10)plot(wc)
Global Summary of DayAvailable from: ftp://ftp.ncdc.noaa.gov/pub/data/gsod/These data are massive.Use the weatherData package to download these data.
Raster package jacob

More Related Content

PDF
Climate data in r with the raster package
PDF
FOSDEM 2015: Distributed Tile Processing with GeoTrellis and Spark
PDF
Q4 2016 GeoTrellis Presentation
PDF
Analyzing Larger RasterData in a Jupyter Notebook with GeoPySpark on AWS - FO...
PPTX
Deep Learning on Aerial Imagery: What does it look like on a map?
PDF
Cassandra&map reduce
PDF
Amazon-style shopping cart analysis using MapReduce on a Hadoop cluster
PDF
GeoSpatially enabling your Spark and Accumulo clusters with LocationTech
Climate data in r with the raster package
FOSDEM 2015: Distributed Tile Processing with GeoTrellis and Spark
Q4 2016 GeoTrellis Presentation
Analyzing Larger RasterData in a Jupyter Notebook with GeoPySpark on AWS - FO...
Deep Learning on Aerial Imagery: What does it look like on a map?
Cassandra&map reduce
Amazon-style shopping cart analysis using MapReduce on a Hadoop cluster
GeoSpatially enabling your Spark and Accumulo clusters with LocationTech

What's hot (20)

PDF
Processing Geospatial Data At Scale @locationtech
PPTX
SparkNotes
PDF
[@NaukriEngineering] Apache Spark
PPTX
Stratosphere with big_data_analytics
PPTX
LocationTech Projects
PDF
Applying stratosphere for big data analytics
PPTX
2021 Dask Summit - Using STAC to catalog SpatioTemporal datasets
PPTX
Mastering Hadoop Map Reduce - Custom Types and Other Optimizations
PDF
Big Data Analytics with Scala at SCALA.IO 2013
PPT
Hadoop institutes-in-bangalore
PPT
chapter - 6.ppt
PPTX
MapReduce basic
PPTX
Bioclouds CAMDA (Robert Grossman) 09-v9p
PDF
Locality Sensitive Hashing By Spark
PPTX
AWS Roadshow Herbst 2013: Datenanalyse und Business Intelligence
PDF
Raster Processing with Scipy.ndimage (Dev Meet Up II)
PDF
The maths behind microscaling
PPTX
Streaming API, Spark and Ruby
PPTX
Weather Data Analytics Using Hadoop
PPTX
A Parallel Algorithm for Approximate Frequent Itemset Mining using MapReduce
Processing Geospatial Data At Scale @locationtech
SparkNotes
[@NaukriEngineering] Apache Spark
Stratosphere with big_data_analytics
LocationTech Projects
Applying stratosphere for big data analytics
2021 Dask Summit - Using STAC to catalog SpatioTemporal datasets
Mastering Hadoop Map Reduce - Custom Types and Other Optimizations
Big Data Analytics with Scala at SCALA.IO 2013
Hadoop institutes-in-bangalore
chapter - 6.ppt
MapReduce basic
Bioclouds CAMDA (Robert Grossman) 09-v9p
Locality Sensitive Hashing By Spark
AWS Roadshow Herbst 2013: Datenanalyse und Business Intelligence
Raster Processing with Scipy.ndimage (Dev Meet Up II)
The maths behind microscaling
Streaming API, Spark and Ruby
Weather Data Analytics Using Hadoop
A Parallel Algorithm for Approximate Frequent Itemset Mining using MapReduce
Ad

Similar to Raster package jacob (20)

PDF
Spatial Analysis with R - the Good, the Bad, and the Pretty
PDF
Essentials of R
PDF
Pollution
PDF
Spatial Data Science with R
DOCX
R Data Visualization-Spatial data and Maps in R: Using R as a GIS
PDF
Finding Meaning in Points, Areas and Surfaces: Spatial Analysis in R
PPTX
Exploratory Analysis Part1 Coursera DataScience Specialisation
PPT
STATISTICAL ANALYSIS FOR NEW STUDENTS mueller.ppt
PPTX
Using R to Visualize Spatial Data: R as GIS - Guy Lansley
ODP
Geospatial Data in R
PPTX
R spatial presentation
PPT
Applied spatial data introducing
PPTX
Dr Richard Fry - Using R as a GIS
PPTX
R programming language in spatial analysis
PDF
rworldmap: A New R package for Mapping Global Data
PDF
Advancing Spatio-temporal Analysis of Ecological Data Examples in R.pdf
PDF
Data Visualization Techniques in Meteorological and Climatological World usin...
PDF
R for the Rest of Us A Statistics Free Introduction David Keyes
PPTX
Sophisticated tools for spatio-temporal data exploration
PDF
Importing satellite imagery into R from NASA and the U.S. Geological Survey
Spatial Analysis with R - the Good, the Bad, and the Pretty
Essentials of R
Pollution
Spatial Data Science with R
R Data Visualization-Spatial data and Maps in R: Using R as a GIS
Finding Meaning in Points, Areas and Surfaces: Spatial Analysis in R
Exploratory Analysis Part1 Coursera DataScience Specialisation
STATISTICAL ANALYSIS FOR NEW STUDENTS mueller.ppt
Using R to Visualize Spatial Data: R as GIS - Guy Lansley
Geospatial Data in R
R spatial presentation
Applied spatial data introducing
Dr Richard Fry - Using R as a GIS
R programming language in spatial analysis
rworldmap: A New R package for Mapping Global Data
Advancing Spatio-temporal Analysis of Ecological Data Examples in R.pdf
Data Visualization Techniques in Meteorological and Climatological World usin...
R for the Rest of Us A Statistics Free Introduction David Keyes
Sophisticated tools for spatio-temporal data exploration
Importing satellite imagery into R from NASA and the U.S. Geological Survey
Ad

More from CCAFS | CGIAR Research Program on Climate Change, Agriculture and Food Security (20)

PPTX
Achieving NDC Ambition in Agriculture: How much does agriculture contribute t...
PPTX
Achieving NDC Ambition in Agriculture: Mitigation ambition in new & updated N...
PPTX
Achieving NDC Ambition in Agriculture: Overview of NDC ambition in the agricu...
PDF
CSA Monitoring: Understanding adoption, synergies and tradeoffs at farm and h...
PPTX
Livestock and sustainability in changing climate: Impacts and global best pra...
PPTX
ADB ESLAP case study outputs and synthesis results: Sustainable livestock gui...
PDF
ADB ESLAP Case Study "Dairy value chain in Indonesia"
PPTX
Assessment of the environmental sustainability of plant-based meat and pork: ...
PPTX
PDF
The impact of mechanization in smallholder rice production in Nigeria
PPTX
PPTX
Greenhouse gas (GHG) emissions & priority action in climate mitigation in the...
PDF
Evaluation of Rwanda climate services for agriculture through a gender lens
PDF
Introduction to Climate-Smart Agriculture: Busia County, Kenya
PPTX
Delivering information for national low-emission development strategies: acti...
PPTX
Delivering information for national low-emission development strategies: acti...
Achieving NDC Ambition in Agriculture: How much does agriculture contribute t...
Achieving NDC Ambition in Agriculture: Mitigation ambition in new & updated N...
Achieving NDC Ambition in Agriculture: Overview of NDC ambition in the agricu...
CSA Monitoring: Understanding adoption, synergies and tradeoffs at farm and h...
Livestock and sustainability in changing climate: Impacts and global best pra...
ADB ESLAP case study outputs and synthesis results: Sustainable livestock gui...
ADB ESLAP Case Study "Dairy value chain in Indonesia"
Assessment of the environmental sustainability of plant-based meat and pork: ...
The impact of mechanization in smallholder rice production in Nigeria
Greenhouse gas (GHG) emissions & priority action in climate mitigation in the...
Evaluation of Rwanda climate services for agriculture through a gender lens
Introduction to Climate-Smart Agriculture: Busia County, Kenya
Delivering information for national low-emission development strategies: acti...
Delivering information for national low-emission development strategies: acti...

Recently uploaded (20)

DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Encapsulation theory and applications.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
KodekX | Application Modernization Development
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
The AUB Centre for AI in Media Proposal.docx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Spectral efficient network and resource selection model in 5G networks
Per capita expenditure prediction using model stacking based on satellite ima...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Programs and apps: productivity, graphics, security and other tools
Building Integrated photovoltaic BIPV_UPV.pdf
Approach and Philosophy of On baking technology
Advanced methodologies resolving dimensionality complications for autism neur...
Review of recent advances in non-invasive hemoglobin estimation
Digital-Transformation-Roadmap-for-Companies.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Encapsulation theory and applications.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Chapter 3 Spatial Domain Image Processing.pdf
KodekX | Application Modernization Development
Unlocking AI with Model Context Protocol (MCP)
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

Raster package jacob

  • 1. Climate data in R with the raster packageJacob van Etten
  • 2. PackagesThere 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.
  • 3. Get raster in RStudioClick 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.
  • 4. Load the packageWith 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")Thesecondfunctiongivesyouanoverview of thefunctions in thepackage.
  • 5. The raster() functionThe 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.
  • 6. Adding valuesHow 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
  • 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 options that plot() gives.?plotClick “Plot a Raster* object”.Also, take a look at the examples and try some if you want.
  • 8. Real dataLet’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”)
  • 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. 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))Orchooseyourownarea of interest, forinstanceusing Google Earth.Then cut the new extent out of r2 and visualize.r3 <- crop(r2, newExtent)plot(r3)
  • 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 and why?r5 <- r2 + r3
  • 12. Some operationsAggregating 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.
  • 13. InterpolationSee if you can work this out for yourself.Take a look at the first example of ?interpolate
  • 14. Sources of dataFor an overview of a lot of relevant climate and weather data, visit this website:http://iridl.ldeo.columbia.edu/
  • 15. 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
  • 16. WorldclimPrecipitation at 10 minute resolutionwc <- getData(“worldclim”, var=“prec”, res=10)plot(wc)
  • 17. Global Summary of DayAvailable from: ftp://ftp.ncdc.noaa.gov/pub/data/gsod/These data are massive.Use the weatherData package to download these data.