Geospatial Data Analysis with R
Last Updated :
24 Apr, 2025
Geospatial data analysis involves working with data that has a geographic or spatial component. It allows us to analyze and visualize data in the context of its location on the Earth's surface. R Programming Language is a popular open-source programming language, that offers a wide range of packages and tools for geospatial data analysis.
fundamental concepts of Geospatial data
1. Spatial Data Types
Vector Data: Geospatial vector data represent discrete geographical features as points, lines, and polygons. Points can be used for locations, lines for linear features like roads, and polygons for areas such as administrative boundaries.
Raster Data: Raster data represents geographic features as a grid of cells, where each cell has a value. This type of data is suitable for continuous or gridded data, such as satellite imagery and elevation data.
2. Coordinate Reference Systems (CRS):
Geospatial data relies on coordinate reference systems to define locations on the Earth's surface. Common CRS include WGS84 (GPS coordinates) and UTM (Universal Transverse Mercator). You must understand CRS to work with geospatial data effectively.
3. Geospatial Packages:
R offers several packages for geospatial data analysis, including:
- sf (Simple Features): This package provides a modern, efficient data structure for representing spatial data.
- rgdal: It offers functions to read and write geospatial data in various formats.
- sp: This package provides classes and methods for spatial data manipulation and visualization.
- raster: It is used for working with raster data and performing spatial operations on grids.
Example 1: Importing and Plotting Spatial Data (Vector)
R
# Load the sf package
library(sf)
# Read a shapefile containing polygon data
world <- st_read(system.file("shape/nc.shp", package="sf"))
# Plot the spatial data
plot(world)
Output:
Introduction to Geospatial Data Analysis with R
we load the sf package and read a shapefile containing polygon data representing North Carolina counties. We then plot the spatial data using the plot() function.
Example 2: Raster Data Analysis
R
# Load required packages
library(raster)
# Read a raster dataset (elevation data)
elevation <- raster(system.file("external/test.grd", package="raster"))
# Compute statistics on the elevation data
elev_summary <- summary(elevation)
# Display the summary statistics
print(elev_summary)
Output:
test
Min. 138.7071
1st Qu. 293.9575
Median 371.9001
3rd Qu. 501.0102
Max. 1736.0580
NA's 6022.0000
In this example, we use the raster package to read a raster dataset containing elevation data. We then compute summary statistics, such as minimum, maximum, and mean elevation, using the summary() function. The result, elev_summary, provides information about the elevation dataset.
Plot the graph for Geospatial Data in R
R
# Install and load necessary packages if you haven't already
install.packages("ggplot2")
install.packages("maps")
install.packages("plotly")
library(ggplot2)
library(maps)
library(plotly)
# Load earthquake data from the 'quakes' dataset
data(quakes)
# Create a basic map of earthquake occurrences
world_map <- map_data("world")
ggplot() +
geom_polygon(data = world_map, aes(x = long, y = lat, group = group),
fill = "white", color = "black") +
geom_point(data = quakes, aes(x = long, y = lat, size = mag,
color = depth), alpha = 0.7) +
scale_size_continuous(range = c(1, 10)) +
scale_color_gradient(low = "blue", high = "red") +
labs(
title = "Global Earthquake Occurrences",
subtitle = "Magnitude and Depth",
x = "",
y = ""
) +
theme_void() +
theme(plot.title = element_text(hjust = 0.5, size = 18),
plot.subtitle = element_text(hjust = 0.5, size = 14))
# Make the plot interactive using plotly
earthquake_plot <- ggplotly()
# Display the interactive plot
earthquake_plot
Output:
Introduction to Geospatial Data Analysis with R
- ggplot(): We initialize the plot.
- geom_polygon(): We add a layer for drawing the world map with white fill and black borders.
- geom_point(): We add a layer for plotting earthquake occurrences as points. We map the longitude (long) to the x-axis, latitude (lat) to the y-axis, magnitude (mag) to the size of points, and depth (depth) to the color of points. The alpha parameter controls the transparency of points.
- scale_size_continuous(): We customize the size range of the points.
- scale_color_gradient(): We customize the color scale of the points from blue (low depth) to red (high depth).
- labs(): We set the plot title, subtitle, and axis labels.
- theme_void(): We use a minimal theme with no background.
- theme(plot.title): We further customize the appearance of the plot by adjusting the title's size and position.
Example 3 using rgdal (Reading and Plotting Spatial Data)
R
# Load the rgdal package
library(rgdal)
# Read a shapefile containing polygon data
world <- readOGR(dsn = system.file("vectors", package = "rgdal"), layer = "world")
# Plot the spatial data
plot(world, col = "lightblue", main = "World Map")
Output:
Introduction to Geospatial Data Analysis with R
In this example, we use the rgdal package to read a shapefile containing polygon data representing countries of the world. We use the readOGR() function to read the shapefile, and then we use the plot() function to visualize the spatial data. We specify the col argument to set the fill color for the polygons and add a title to the plot.
Example 4 using sp (Spatial Data Manipulation and Visualization)
R
# Load the sp package
library(sp)
# Create a simple SpatialPointsDataFrame
coords <- data.frame(
x = c(0, 1, 2),
y = c(0, 1, 0)
)
points <- SpatialPoints(coords)
df <- data.frame(ID = c("A", "B", "C"))
spdf <- SpatialPointsDataFrame(points, df)
# Plot the SpatialPointsDataFrame
plot(spdf, pch = 19, col = "red", main = "Simple SpatialPointsDataFrame")
Output:
Introduction to Geospatial Data Analysis with R In this example, we use the sp package to create a simple SpatialPointsDataFrame. We define coordinates (x and y) for three points, create a data frame for attribute data, and then combine them into a SpatialPointsDataFrame. Finally, we use the plot() function to visualize the points. We specify the pch argument to set the point type, the col argument to set the point color, and add a title to the plot.
Similar Reads
What is Geospatial Data Analysis?
Have you ever used a ride-sharing app to find the nearest drivers, pinpointed a meeting location on a map, or checked a weather forecast showing precipitation patterns? If so, you have already interacted with geospatial analysis! This widespread, versatile field integrates geography, statistics, and
11 min read
What is Spatial Analysis?
Have you ever wondered how city planners decide where to build schools, hospitals, or parks? How did authorities track and manage the spread of COVID-19 to contain the outbreak effectively? How are vaccination strategies devised and monitored to ensure equitable distribution? How are such precise ma
9 min read
Graphical Data Analysis in R
Graphical Data Analysis (GDA) is a powerful tool that helps us to visualize and explore complex data sets. R is a popular programming language for GDA as it has a wide range of built-in functions for producing high-quality visualizations. In this article, we will explore some of the most commonly us
7 min read
Analyzing Weather Data in R
Weather data analysis allows us to understand patterns, trends, and anomalies in weather conditions over time. We will explore how to analyze weather data using the R Programming Language. We will use a dataset containing various weather parameters such as temperature, humidity, wind speed, and more
12 min read
Data analysis using R
Data Analysis is a subset of data analytics, it is a process where the objective has to be made clear, collect the relevant data, preprocess the data, perform analysis(understand the data, explore insights), and then visualize it. The last step visualization is important to make people understand wh
10 min read
What is Statistical Analysis in Data Science?
Statistical analysis serves as a cornerstone in the field of data science, providing essential tools and techniques for understanding, interpreting, and making decisions based on data. In this article we are going to learn about the statistical analysis in data science and discuss few types of stati
6 min read
SQL vs R - Which to use for Data Analysis?
Data Analysis, as the name suggests, means the evaluation or examination of the data, in Laymanâs terms. The answer to the question as to why Data Analysis is important lies in the fact that deriving insights from the data and understanding them are extremely crucial for organizations and businesses
5 min read
Free Public Data Sets For Analysis
Data analysis is a crucial aspect of modern decision-making processes across various domains, including business, academia, healthcare, and government. However, obtaining high-quality datasets for analysis can be challenging and costly. Fortunately, there are numerous free public datasets available
5 min read
Working with Geospatial Data in Python
Spatial data, also known as geospatial data, GIS data, or geodata, is a type of numeric data that defines the geographic location of a physical object, such as a building, a street, a town, a city, a country, or other physical objects, using a geographic coordinate system. You may determine not just
14 min read
Exploratory Data Analysis in R Programming
Exploratory Data Analysis or EDA is a statistical approach or technique for analyzing data sets to summarize their important and main characteristics generally by using some visual aids. The EDA approach can be used to gather knowledge about the following aspects of data. Main characteristics or fea
11 min read