Raster Data in R

In this article we will discuss what is Raster Data in R Programming Language and how we use Raster Data in different work scenarios.

What is Raster Data in R?

Raster data, representing spatial data in a grid format, is crucial in fields such as geography, environmental science, and remote sensing. R, a powerful statistical computing language, offers extensive packages and functions for manipulating and analyzing raster data.

Understanding Raster Data

Raster data consists of a matrix of cells (or pixels), each representing a value corresponding to a particular geographic area. Common uses include satellite imagery, climate data, and elevation models. To work with raster data in R, you need to install and load the relevant packages. There are some packages:

  1. raster
  2. rgdal
  3. sp
  4. terra

Plotting Basic Raster Data

Plotting is an essential step to visualize raster data. The plot function from the raster package makes it easy.

R
install.packages("raster")
install.packages("rgdal")
install.packages("sp")
install.packages("terra")

library(raster)
library(rgdal)
library(sp)
library(terra)

# Define the number of rows and columns
nrows <- 100
ncols <- 100

# Create an empty raster
r <- raster(nrows=nrows, ncols=ncols, xmn=0, xmx=10, ymn=0, ymx=10)

# Set the CRS (Coordinate Reference System)
crs(r) <- CRS("+proj=longlat +datum=WGS84")

# Populate the raster with random values
values(r) <- runif(ncell(r), min=0, max=100)

# Plot the raster
plot(r, main="Random Raster Data")

Output:

Raster Data in R

A graphical window will open displaying the raster data with random values. The main title will be “Random Raster Data”.

Enhanced Raster Plotting with Custom Color Scales

In R, the raster package provides powerful tools for working with raster data, including the ability to create custom color scales for raster plotting.

R
# Create a sample raster
values(r) <- runif(ncell(r), min=0, max=100)

# Custom color scale
colors <- colorRampPalette(c("blue", "green", "yellow", "red"))

# Enhanced plot
plot(r, col=colors(100), main="Enhanced Raster Plot with Custom Color Scale")

Output:

Raster Data in R

It displaying the raster data with a custom color scale ranging from blue to red. The main title will be “Enhanced Raster Plot with Custom Color Scale”.

Plotting Raster with Contour Lines

In R, you can plot raster data with contour lines using functions from the raster package. The contour() function generates contour lines from raster data, and the plot() function overlays the contour lines on the raster plot.

R
# Create a sample raster
values(r) <- runif(ncell(r), min=0, max=100)

# Plot raster with contour lines
plot(r, col=colors(100), main="Raster Plot with Contour Lines")
contour(r, add=TRUE)

Output:

Raster Data in R

A graphical window will open displaying the raster data with contour lines overlaid. The main title will be “Raster Plot with Contour Lines”.

Conclusion

Working with raster data in R is facilitated by powerful packages like raster, rgdal, sp, and terra. From basic operations such as reading, plotting, and accessing cell values to more advanced techniques like cropping, masking, reprojection, aggregation, resampling, and extracting values by points, R provides comprehensive tools for spatial data analysis. Whether you’re involved in environmental science, remote sensing, or any field that requires spatial data analysis, mastering raster data manipulation in R is invaluable.