Plotting geom_tiles in ggmap using a data frame
In this article, we will explore how to plot geom_tiles in ggmap using a data frame. The goal is to create a map with tiles that represent the values from our data.
Introduction
ggmap is a powerful R package for creating maps. It allows us to easily add maps to our plots and customize various aspects of the map, such as the tile layer, theme, and more. In this article, we will focus on plotting geom_tiles in ggmap using a data frame.
Background
Before diving into the code, let’s first understand what geom_tiles is. GeomTile is a geometric object that represents a tile on a map. It has an x-coordinate, y-coordinate, and fill value. In the context of plotting tiles in ggmap, we use the geom_tile() function to create these tiles.
The Problem
The issue with plotting tiles using geom_tiles is that it’s based on continuous scales, which means the tiles are too small to be visible. To fix this, we need to adjust the scale of our data or change the way we plot the tiles.
Solution
To make the tiles visible, we can use the following approaches:
- Set the
widthandheightparameters when callinggeom_tile(). This will create larger tiles that are more visible. - Use
geom_point()with a rectangle shape instead ofgeom_tile(). This will allow us to plot individual points on the map, which can be used to create the tile effect.
Code
Here’s an example code snippet that demonstrates how to plot geom_tiles using a data frame:
# Load necessary libraries
library(ggplot2)
library(ggmap)
# Define the tile coordinates
gtha <- c(left = -79.95306, bottom = 43.27577, right = -78.91296, top = 43.95626)
# Get a Stamen map with the specified tile coordinates
map <- get_stamenmap(gtha, maptype = "toner-lite", crop = FALSE)
# Create a ggplot object from the map
p <- ggmap(map) +
theme_void()
# Plot tiles using geom_tile() with width and height parameters
p +
geom_tile(data = test, aes(x = X, y = Y, fill = final), height = .1, width = .1)
# Alternatively, use geom_point() to plot individual points on the map
p +
geom_point(data = test, aes(x = X, y = Y, fill = final), shape = 22)
Data
Here’s an example data frame that we can use for plotting tiles:
test <- structure(list(final = c(5.699134, 0, 0, 5.111695, 2.818152,
5.667874),
Month_Yr = c("2018-12", "2018-12", "2018-12", "2018-12",
"2018-12", "2018-12"),
X = c(-79.83759, -79.58547, -79.69059,
-79.88913, -79.34186, -79.83636),
Y = c(43.28888, 43.89921, 43.87526,
43.302, 43.65824, 43.28887)),
class = "data.frame", row.names = c("2575548",
"2575550", "2575552", "2575554", "2575555", "2575556"))
Conclusion
In this article, we explored how to plot geom_tiles in ggmap using a data frame. We discussed the issue of continuous scales and provided two approaches to fix it: setting the width and height parameters or using geom_point() with a rectangle shape. We also included an example code snippet and data frame that demonstrates how to plot tiles using these approaches.
Last modified on 2023-10-21