Filtering Polygons in a Map using R: A Step-by-Step Guide

Filtering Polygons in a Map using R

In this article, we will explore how to filter only one polygon in a map in R. We will use the rgdal package for reading and plotting shapefiles, as well as the subset function from base R.

Introduction

Geographic Information Systems (GIS) are widely used tools for analyzing and visualizing geographic data. One common task in GIS is to filter or subset a dataset based on specific criteria. In this article, we will focus on filtering polygons in a map using R.

We will use the rgdal package to read and plot shapefiles, as well as the subset function from base R for more flexible filtering.

Background

Shapefiles are a popular format for storing and exchanging geographic data. They contain a set of points, lines, or polygons that represent geographical features such as cities, roads, or borders. The rgdal package provides a convenient interface for reading and manipulating shapefiles in R.

The subset function is a powerful tool for filtering datasets in R. It allows you to select specific rows based on conditions specified using logical expressions. In this article, we will use the subset function to filter polygons in a map.

Installing Required Packages

Before we begin, make sure you have the required packages installed. You can install them using the following command:

install.packages(c("rgdal", "ggplot2"))

Reading and Plotting a Shapefile

Let’s start by reading and plotting a shapefile.

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

# Create temporary directories for downloading and storing the shapefile
temp <- tempfile()
temp2 <- tempfile()

# Download the shapefile from the internet
download.file("https://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_municipais/municipio_2015/UFs/PR/pr_municipios.zip", temp)

# Unzip the downloaded zip file
unzip(zipfile = temp, exdir = temp2)

# Read the shapefile using rgdal
shp <- readOGR(temp2)

In this code block, we first load the required libraries. We then create temporary directories for downloading and storing the shapefile.

Next, we download the shapefile from the internet using the download.file function.

We then unzip the downloaded zip file using the unzip function.

Finally, we read the shapefile using the readOGR function from the rgdal package.

Plotting the Shapefile

Now that we have read the shapefile, let’s plot it using the plot function.

# Create a new figure with a set of default parameters
plot(shp)

# Add title and labels to the plot
title("Map of Ponta Grossa")
legend("bottomright", c("Ponta Grossa"), lty = 1, col = "blue")

In this code block, we create a new figure with a set of default parameters using the plot function.

We then add title and labels to the plot using the title, legend, and col arguments.

Filtering Polygons

Now that we have plotted the shapefile, let’s filter it by selecting only the polygon corresponding to Ponta Grossa.

# Filter polygons with NM_MUNICIP equal to "Ponta Grossa"
shp_subset <- shp[shp$NM_MUNICIP == "PONTA GROSSA",]

# Plot the filtered shapefile
plot(shp_subset)

In this code block, we use square bracket notation to select rows from the shp data frame based on the condition shp$NM_MUNICIP == "PONTA GROSSA".

We then plot the filtered shapefile using the plot function.

Alternatively, you can also use the subset function from base R to filter polygons.

# Filter polygons with NM_MUNICIP equal to "Ponta Grossa"
shp_subset <- subset(shp, NM_MUNICIP == "PONTA GROSSA")

# Plot the filtered shapefile
plot(shp_subset)

In this code block, we use the subset function from base R to select rows from the shp data frame based on the condition NM_MUNICIP == "PONTA GROSSA".

We then plot the filtered shapefile using the plot function.

Conclusion

In this article, we learned how to filter only one polygon in a map using R. We used the rgdal package for reading and plotting shapefiles, as well as the subset function from base R for more flexible filtering.


Last modified on 2024-06-23