Spatial Visualization with R and ggmap
The ggmap
package enables the visualization of spatial data and spatial statistics in a map format using the layered approach of ggplot2
. This package also includes basemaps that give your visualizations context including Google Maps, Open Street Map, Stamen Maps, and CloudMade maps. In addition, utility functions are provided for accessing various Google services including Geocoding, Distance Matrix, and Directions.
The ggmap
package is based on ggplot2
, which means it will take a layered approach and will consist of the same five components found in ggplot2
. These include a default dataset with aesthetic mappings where x is longitude, y is latitude, and the coordinate system is fixed to Mercator. Other components include one or more layers defined with a geometric object and statistical transformation, a scale for each aesthetic mapping, coordinate system, and facet specification. Because ggmap
is built on ggplot2
it has access to the full range of ggplot2
functionality.
In this exercise you’ll learn how to use the ggmap
package to plot various types of spatial visualizations.
Step 1: Creating a Basemap
There are two basic steps to create a map with ggmap
. The details are more complex than these two steps might imply, but in general you just need to download the map raster and then plot operational data on the basemap. Step 1 is to download the map raster, also known as the basemap. This is accomplished using the get_map()
function, which can be used to create a basemap from Google, Stamen, Open Street Map, or CloudMade. You’ll learn how to do that in this step. In a future step you’ll learn how to add and style operational data in various ways.
library(ggplot2) library(ggmap) library(readr) library(dplyr)
myLocation
and set it to California
. Call the get_map()
function with a zoom level of 6, and plot the map using the ggmap()
function, passing in a reference to the variable returned by the get_map()
function. The default map type is Google Maps with a style of Terrain.myLocation = 'California' myMap = get_map(location = myLocation, zoom = 6) ggmap(myMap)
The Google source includes a number of map types including those you see in the screenshot below.
myMap = get_map(location = myLocation, zoom = 6, source="google", maptype="satellite") ggmap(myMap)
myMap = get_map(location = "Grand Canyon, Arizona", zoom = 11) ggmap(myMap)
The zoom level can be set between 3 and 21 with 3 representing a continent level view, and 21 representing a building level view.
Step 2: Adding Operational Data Layers
ggmap()
returns a ggplot
object, meaning that it acts as a base layer in the ggplot2
framework. This allows for the full range of ggplot2
capabilities meaning that you can plot points on the map, add contours and 2D heat maps, and more. We’ll exam-ine some of these capabilities in this section. 1. For this section we’ll use the historical wildfire information found in the StudyArea.csv file. Load this dataset using the read_csv()
function. You can download this file at: https://www.dropbox.com/s/9ouh21a6ym62nsl/StudyArea.csv?dl=0
dfWildfires = read_csv("~/Desktop/IntroR/Data/StudyArea.csv", col_types = list(FIRENUMBER = col_character(), UNIT = col_character()), col_names = TRUE)
myLocation = 'California' #get the basemap myMap = get_map(location = myLocation, zoom = 6) # use the select() function to limit the columns from the data frame df = select(dfWildfires, STATE, YEAR_, TOTALACRES, DLATITUDE, DLONGITUDE) #use the filter() function to get only fires in California with acres #burned greater than 1000 df = filter(df, TOTALACRES >= 1000 & STATE == 'California') #produce the final map ggmap(myMap) + geom_point(data=df, aes(x = DLONGITUDE, y = DLATITUDE))
dplyr
mutate()
function to group the fires by decade.df = mutate(df, DECADE = ifelse(YEAR_ %in% 1980:1989, "1980-1989", ifelse(YEAR_ %in% 1990:1999, "1990-1999", ifelse(YEAR_ %in% 2000:2009, "2000-2009", ifelse(YEAR_ %in% 2010:2016, "2010-2016", "-99")))))
DECADE
and create a graduated symbol map based on the size of each fire. The colour
property defines the column to use for grouping, and the size
property defines the column to use for the size of each symbol.ggmap(myMap) + geom_point(data=df, aes(x = DLONGITUDE, y = DLATITUDE, colour= DECADE, size = TOTALACRES))
myMap = get_map(location = "Santa Clarita, California", zoom = 10) ggmap(myMap) + geom_point(data=df, aes(x = DLONGITUDE, y = DLATITUDE, colour= DECADE, size = TOTALACRES))
geom_density2d()
function is used to create the contours while the stat_density2d()
function creates the heat map. Add the following code to produce the map you see below. You can experiment with the colors using the scale_fill_gradient(low and high)
properties. Here we’ve set them to green and red respectively, but you may want to change the color scheme.myMap = get_map(location = "Santa Clarita, California", zoom = 8) ggmap(myMap, extent = "device") + geom_density2d(data = df, aes(x = DLONGITUDE, y = DLATITUDE), size = 0.3) + stat_density2d(data = df, aes(x = DLONGITUDE, y = DLATITUDE, fill = ..level.., alpha = ..level..), size = 0.01, bins = 16, geom = "polygon") + scale_fill_gradient(low = "green", high = "red") + scale_alpha(range = c(0, 0.3), guide = FALSE)
ggmap(myMap, extent = "device") + stat_density2d(data = df, aes(x = DLONGITUDE, y = DLATITUDE, fill = ..level.., alpha = ..level..), size = 0.01,bins = 16, geom = "polygon") + scale_fill_gradient(low = "green", high = "red") + scale_alpha(range = c(0, 0.3), guide = FALSE)
df = filter(dfWildfires, STATE == 'California') df = filter(df, YEAR_ %in% c(2010, 2011, 2012, 2013, 2014, 2015, 2016)) myMap = get_map(location = "Santa Clarita, California", zoom = 9) ggmap(myMap, extent = "device") + stat_density2d(data = df, aes(x = DLONGITUDE, y = DLATITUDE, fill = ..level.., alpha = ..level..), size = 0.01,bins = 16, geom = "polygon") + scale_fill_gradient(low = "green", high = "red") + scale_alpha(range = c(0, 0.3), guide = FALSE) + facet_wrap(~ YEAR_)
ncG1vNJzZmirY2Ourq3ZqKWar6NjsLC5jo%2Bgq6yllrmIlbJ8o5qro6e8sLmOgqWtqp%2BHfI%2B7056ZqKebqHyUvMCtoJqkhp7Atq3LorGarJmku5OrxqCkmqhencGuuA%3D%3D