Origins

This tutorial is an interpretation of the neon data skills tutorial at: http://neondataskills.org/R/Raster-Data-In-R/

Load Libraries

The basic libraries needed for working with rasters in R are raster, sp, and rgdal.

library(sp)
library(rgdal)
## rgdal: version: 1.2-5, (SVN revision 648)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.0.1, released 2015/09/15
##  Path to GDAL shared files: C:/Users/Gregory/Documents/R/win-library/3.3/rgdal/gdal
##  Loaded PROJ.4 runtime: Rel. 4.9.2, 08 September 2015, [PJ_VERSION: 492]
##  Path to PROJ.4 shared files: C:/Users/Gregory/Documents/R/win-library/3.3/rgdal/proj
##  Linking to sp version: 1.2-4
library(raster)
library(ggplot2)
library(rasterVis)
## Loading required package: lattice
## Loading required package: latticeExtra
## Loading required package: RColorBrewer
## 
## Attaching package: 'latticeExtra'
## The following object is masked from 'package:ggplot2':
## 
##     layer

Load Raster

dem <- raster('20170112222539_1989219947.tif')
dem
## class       : RasterLayer 
## dimensions  : 1640, 2195, 3599800  (nrow, ncol, ncell)
## resolution  : 0.0002777778, 0.0002777778  (x, y)
## extent      : -118.789, -118.1793, 33.97514, 34.43069  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
## data source : C:\Users\Gregory\RStudioProjects\raster_tutorial\20170112222539_1989219947.tif 
## names       : X20170112222539_1989219947 
## values      : -32768, 32767  (min, max)

Min/Max Values

Use minValue and maxValue to check the range of values in the raster.

minValue(dem)
## [1] -32768

Use maxValue to check

maxValue(dem)
## [1] 32767

Coordinate Reference System

Check the projection using crs.

crs(dem)
## CRS arguments:
##  +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0

Extent

Check the extent of the raster using extent.

extent(dem)
## class       : Extent 
## xmin        : -118.789 
## xmax        : -118.1793 
## ymin        : 33.97514 
## ymax        : 34.43069

Plotting

Plot the raster using plot.

plot(dem, col=terrain.colors(30))

Or using image.

image(dem, col=terrain.colors(30))

Or using gplot.

theme_set(theme_bw())
gplot(dem) + 
  geom_tile(aes(fill = value)) +
  facet_wrap(~ variable) +
  scale_fill_gradient(low = 'white', high = 'blue') +
  coord_equal()

Distribution

Check the distribution of values in the raster using hist.

## Warning in .hist1(x, maxpixels = maxpixels, main = main, plot = plot, ...):
## 3% of the raster cells were used. 100000 values used.