Displaying Information on a Map Using R and rgdal Library

Displaying Information on a Map

Overview

In this article, we will explore the process of displaying information on a map using R and the rgdal library. We will also cover how to write the name of each region on the map and present data in a heatmap format.

Prerequisites

To follow along with this tutorial, you will need:

  • R installed on your system
  • The rgdal library installed using install.packages("rgdal")
  • A basic understanding of R programming language

Installing Required Libraries

Before we begin, ensure that the required libraries are installed. If not, install them now.

# Install the required libraries
install.packages("rgdal")

Loading Required Libraries

Load the rgdal library using:

# Load the required libraries
library(rgdal)
library(RColorBrewer)
library(classInt)
library(ggplot2)

The data used in this tutorial is available for download from here.

Loading the Data

Load the nypp_15b shapefile into R using:

# Load the shapefile
download.file(
    "http://www.rob-barry.com/assets/data/mapping/nypp_15b.zip",
    destfile = "nypp_15b.zip"
)

unzip(zipfile = "nypp_15b.zip")

nypp <- readOGR("nypp_15b", "nypp")

Displaying the Shapefile

To display the shapefile, use:

# Display the shapefile
plot(nypp)

Creating a Heatmap

To create a heatmap of safpct, use:

# Load necessary libraries and data
library(RColorBrewer)
library(classInt)

saf2014 <- read.csv("data/analysis_and_planning/2014_sqf_csv.zip")

# Convert saf2014 to long format using plyr library or data.table
library(plyr)

safpct <- as.data.frame(table(saf2014$pct))

colnames(safpct) <- c("pct", "stops")

# Create a function to merge two dataframes based on a common column
merge.shpdf.df <- function(shpdf, df, by.shpdf, by.df) {
    shpdf@data <- data.frame(shpdf@data, df[match(shpdf@data[, by.shpdf], df[, by.df]), ])
    return(shpdf)
}

# Merge nypp and safpct based on stops
nypp.merge <- merge.shpdf.df(nypp, safpct, "stops", "pct")

# Create a color palette with 5 colors using RColorBrewer library
pal <- brewer.pal(5, "YlOrRd")

# Find class intervals for the stops column
fill.clr <- findColours(classIntervals(nypp.merge@data$stops, style = "pretty", n = 5), pal)

# Plot nypp with fill color based on stops using ggplot2 library
ggplot(nypp.merge, aes(x = long, y = lat, group = group, fill = stops)) +
    geom_polygon() +
    scale_fill_brewer(palette = pal) +
    theme_void()

Displaying Region Names on the Map

To display region names on the map, use:

# Load necessary libraries and data
library(rgdal)

nypp <- readOGR("data/nypp_15b.zip", "nypp")

# Create a function to merge two dataframes based on stops
merge.shpdf.df <- function(shpdf, df, by.shpdf, by.df) {
    shpdf@data <- data.frame(shpdf@data, df[match(shpdf@data[, by.shpdf], df[, by.df]), ])
    return(shpdf)
}

# Merge nypp and safpct based on stops
nypp.merge <- merge.shpdf.df(nypp, saf2014, "stops", "pct")

# Plot nypp with fill color based on stops using ggplot2 library
ggplot(nypp.merge, aes(x = long, y = lat, group = group, fill = stops)) +
    geom_polygon() +
    scale_fill_brewer(palette = pal) +
    theme_void()

# Add labels to the map using ggplot2 library
ggplot(nypp.merge, aes(x = long, y = lat, group = group, fill = stops)) +
    geom_polygon() +
    scale_fill_brewer(palette = pal) +
    theme_void() +
    labs(x = "Longitude", y = "Latitude") +
    theme(legend.position = c(0.9, 0))

# Create a map with region names
map <- ggplot(data.frame(long = long(nypp), lat = lat(nypp), group = group(nypp)), aes(x = long, y = lat)) +
    geom_point() +
    theme_void()

for (i in unique(group(nypp))) {
    map + geom_label(aes(label = group[nypp == i]), check_overlap = TRUE) +
        theme(legend.position = c(0.9, 0))
}

# Display the region names map
print(map)

Displaying Pickup Data Points on the Map

To display coord data points on the map, use:

# Load necessary libraries and data
library(ggplot2)

# Plot long and lat data using ggplot2 library
ggplot(coord, aes(x = pickuplong, y = pickuplat)) +
    geom_point()

Displaying Heatmap of Pickup Data Points

To display a heatmap of coord, use:

# Load necessary libraries and data
library(ggplot2)

# Plot long and lat data using ggplot2 library
ggplot(coord, aes(x = pickuplong, y = pickuplat)) +
    geom_point() +
    theme_void()

Conclusion

In this tutorial, we have explored the process of displaying information on a map using R and the rgdal library. We have covered how to write the name of each region on the map and present data in a heatmap format. This tutorial should give you an understanding of how to create interactive maps with R and rgdal.


Last modified on 2025-02-17