HappiomHappiom
  • Self-Improvement
  • Relationship
  • AI for Life
  • Apps
  • Tech
  • More
    • Online Diary
    • Glossary
  • Learn
    • Book
    • >> Soft Skills
    • Time Management
    • >> Tech Skills
    • R
    • Linux
    • Python
  • Our Apps
    • Download Diary App
    • Write Your First Diary
    • Login to Online Diary App
    • 100K+ Famous Quotes Site
  • Resources
    • Self-Improvement Guide
      • 21-Days to Self-Improvement
      • Creating a Habit
      • Learn Life Experiences
      • Easily Prioritizing Tasks
      • Learning from Mistakes
      • Doing Regular Exercises
      • Setting Priority for Success
      • Avoiding Common Mistakes
      • Eating Healthy Food Regularly
    • Journaling Guide
      • Online Diary
      • Best Diary Apps
      • Diary Writing Ideas
      • Diary Writing Topics
      • Avoid Writing in Diary
      • Diary Writing as Hobby
      • Reasons to Write a Diary
      • Types of Feelings In Diary
      • Improve Diary Writing Skills
  • Self-Improvement
  • Relationship
  • AI for Life
  • Apps
  • Tech
  • More
    • Online Diary
    • Glossary
  • Learn
    • Book
    • >> Soft Skills
    • Time Management
    • >> Tech Skills
    • R
    • Linux
    • Python
  • Our Apps
    • Download Diary App
    • Write Your First Diary
    • Login to Online Diary App
    • 100K+ Famous Quotes Site
  • Resources
    • Self-Improvement Guide
      • 21-Days to Self-Improvement
      • Creating a Habit
      • Learn Life Experiences
      • Easily Prioritizing Tasks
      • Learning from Mistakes
      • Doing Regular Exercises
      • Setting Priority for Success
      • Avoiding Common Mistakes
      • Eating Healthy Food Regularly
    • Journaling Guide
      • Online Diary
      • Best Diary Apps
      • Diary Writing Ideas
      • Diary Writing Topics
      • Avoid Writing in Diary
      • Diary Writing as Hobby
      • Reasons to Write a Diary
      • Types of Feelings In Diary
      • Improve Diary Writing Skills
Expand All Collapse All
  • R Code Examples
    • R Code to Create and Manipulate Vectors
    • R Code to Work with Data Frames
    • R Code to Handle Factors and Categorical Data
    • Example R Code for Basic Data Visualization with ggplot2
    • R Code to Aggregate Data Using dplyr
    • R Code to Apply Functions with lapply and sapply
    • R Code to Handle Missing Data
    • Example R Code for String Manipulation with stringr
    • R Code to Transform Data with tidyr
    • R Code to Perform ADF Test
    • R Code to Perform Data Import and Export with CSV
    • R Code for Filtering Data
    • R Code for Easily Summarizing Data
    • R Code to Perform Linear Regression for Statistical Analysis
    • R Code to Perform t-tests for Statistical Analysis
    • Example R Code for Time Series Analysis
    • R Code for Doing Web Scraping with Examples
    • R Code to Showcase Geospatial Analysis
    • Example R Code to Filter Multiple Conditions (for Data Manipulation)

R Code to Showcase Geospatial Analysis

Geospatial analysis in R enables the exploration and visualization of spatial data, offering insights into geographical patterns and relationships.

With R’s powerful packages like sf and ggplot2, analyzing spatial data becomes efficient and insightful.

Introduction to Geospatial Analysis

First, I’ll give a brief overview about geospatial analysis.

  • R provides tools to handle various types of spatial data formats, including shapefiles, GeoJSON, and raster data.
  • Packages like sf offer simple features for manipulating and analyzing spatial data efficiently.
  • Using packages like ggplot2, R can create visually appealing maps and plots to represent spatial data.
  • These visualizations can showcase geographical distributions, spatial relationships, and patterns, aiding in better understanding and communication of insights.

Example Code

Now let’s write a simple example of geospatial analysis in R using the ggplot2 and sf (Simple Features) packages. In this example, I’ll show the distribution of earthquakes using a dataset from the US Geological Survey (USGS).

Let’s plot earthquake locations on a map of the world.

# Load required libraries
library(ggplot2)
library(sf)

# Load earthquake data from USGS
earthquakes <- st_read("https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.geojson")

# Check the structure of the data
str(earthquakes)

# Plot the map
world_map <- map_data("world")

ggplot() +
  geom_polygon(data = world_map, aes(x = long, y = lat, group = group), fill = "grey", color = "black") +
  geom_point(data = earthquakes, aes(x = longitude, y = latitude), color = "red", alpha = 0.5) +
  coord_fixed(1.3) +
  labs(title = "Earthquakes Worldwide (Last Month)",
       x = "Longitude",
       y = "Latitude")

Let me also explain the above code.

  • Load the necessary libraries ggplot2 for plotting and sf for working with spatial data.
  • Load earthquake data using st_read() function from the USGS GeoJSON feed. This data includes information about earthquakes worldwide.
  • Inspect the structure of the data using str() function to understand its attributes.
  • Use map_data() function to get the map data, and then use ggplot() to create a blank canvas. We use geom_polygon() to draw the world map with gray fill color and black borders. We use geom_point() to plot earthquake locations with red color and reduced opacity (alpha = 0.5).
  • Adjust the coordinate system using coord_fixed() to ensure proper scaling, and add appropriate labels to the plot using labs().

This code creates a simple visualization of earthquake locations on a map of the world, allowing us to analyze their distribution geospatially.

Another Example Code

I’ll also give you another example which shows geospatial analysis in R, focusing on creating a choropleth map to visualize population density in different regions using a built-in dataset.

# Load required libraries
library(ggplot2)
library(sf)

# Load world map data
world_map <- st_read(system.file("shape/nc.shp", package = "sf"))

# Plot choropleth map of population density
ggplot() +
  geom_sf(data = world_map, aes(fill = AREA)) +
  scale_fill_gradient(low = "lightblue", high = "darkblue", name = "Population Density") +
  labs(title = "Population Density in Different Regions")

Simply, by using the packages like sf and ggplot2, R facilitates efficient data handling and creation of insightful visualizations such as maps and choropleths.

Related Articles
  • R Code to Transform Data with tidyr
  • Example R Code for String Manipulation with stringr
  • R Code to Handle Missing Data
  • R Code to Apply Functions with lapply and sapply
  • R Code to Aggregate Data Using dplyr
  • Example R Code for Basic Data Visualization with ggplot2

No luck finding what you need? Contact Us

Previously
R Code for Doing Web Scraping with Examples
Up Next
Example R Code to Filter Multiple Conditions (for Data Manipulation)
  • About Us
  • Contact Us
  • Archive
  • Hindi
  • Tamil
  • Telugu
  • Marathi
  • Gujarati
  • Malayalam
  • Kannada
  • Privacy Policy
  • Copyright 2025 Happiom. All Rights Reserved.