ggmap
What is ggmap?
ggmap is an R package that makes it easy to retrieve raster map tiles from popular online mapping services such as Google Maps and Stamen Maps, and plot them using the ggplot2 framework. The result is an easy, consistent and modular framework for spatial graphics with several tools for spatial data analysis.
The basic idea driving ggmap is to take a downloaded map image, plot it as a contextual layer using ggplot2, and then plot additional content layers of data, statistics, or models on top of the map. In ggmap this process is broken into two pieces:
- Downloading the images and formatting them for plotting, done with
get_map
- Making the plot with
ggmap
.
qmap
combines these two functions for quick map plotting, and qmplot
attempts to wrap up the entire plotting process into one simple command.
In addition, several utility functions are available in ggmap, which allow you to access the Google Geocoding, Distance Matrix, and Directions APIs.
Example of a basic ggmap implementation
Assuming you already have a data set in place on violent crimes in a city that corresponds with longitude and latitude fields, you can quickly plot it with qmplot:
qmplot(lon, lat, data = violent_crimes, maptype = "toner-lite", color = I("red"))
ggmap visualization. Source: github
Since ggmap is built on top of ggplot2, all your usual ggplot2 functionality (e.g., geoms, polishing, etc.) will work. ggplot2 also provides some unique graphing capabilities to your images within ggmap.
Installation
You can install ggmap from CRAN:
install.packages("ggmap")
library(ggmap)
as well as from Github:
if (!requireNamespace("devtools")) install.packages("devtools")
devtools::install_github("dkahle/ggmap")