Professional Documents
Culture Documents
30.5
alize data and models superimposed with their
basic social landmarks and geographic context
is invaluable. ggmap is a new tool which en-
ables such visualization by combining the spatial
30.0
information of static maps from Google Maps,
OpenStreetMap, Stamen Maps or CloudMade
df$lat
Maps with the layered grammar of graphics im-
plementation of ggplot2. In addition, several
29.5
new utility functions are introduced which allow
the user to access the Google Geocoding, Dis-
tance Matrix, and Directions APIs. The result is
an easy, consistent and modular framework for
spatial graphics with several convenient tools for
29.0
df$lon
Introduction
Figure 1: Typical R areal plot zip codes in the
Visualizing spatial data in R can be a challenging Greater Houston area
task. Fortunately the task is made a good deal eas-
ier by the data structures and plot methods of sp, While these kinds of plots are useful, they are not as
RgoogleMaps, and related packages (Pebesma and informative as we would like in many situations. For
Bivand, 2006), (Bivand, Pebesma, and Gomez-Rubio, instance, when plotting zip codes it is helpful to also
2008), (Loecher and Sense Networks, 2010). Using see major roads and other landmarks which form the
those methods, one can plot the basic geographic in- boundaries of areal units.
formation of (for instance) a shape file containing The situation for point referenced spatial data is
polygons for areal data or points for point referenced often much worse. Since we cant easily contextual-
data. However, compared to specialized geographic ize a scatterplot of points without any background
information systems (GISs) such as ESRIs ArcGIS, information at all, it is common to add points as an
which can plot points, polygons, etc. on top of maps overlay of some areal datawhatever areal data is
and satellite imagery with drag-down menus, these available. The resulting plot looks like Figure 2.
visualizations can be pretty disappointing. This ar-
ticle details some new methods for the visualiza-
30.5
In most cases the plot is understandable to the re- The layered grammar of graphics
searcher who has worked on the problem for some
time but is of hardly any use to his audience, who One advantage of making the plots with ggplot2 is
must work to associate the data of interest with their the layered grammar of graphics on which ggplot2
location. Moreover, it leaves out many practical is based (Wickham, 2010) (Wilkinson, 2005). By defi-
detailsare most of the events to the east or west of nition, the layered grammar demands that every plot
landmark x? Are they clustered around more well- consist of five components :
to-do parts of town, or do they tend to occur in dis-
a default dataset with aesthetic mappings,
advantaged areas? Questions like these cant really
be answered using these kinds of graphics because one or more layers, each with a geometric
we dont think in terms of small scale areal bound- object ("geom"), a statistical transformation
aries (e.g. zip codes or census tracts). ("stat"), and a dataset with aesthetic mappings
This is of course a (gross) exaggeration. With a (possibly defaulted),
little effort better plots can be made, and tools such
a scale for each aesthetic mapping (which can
as maps, maptools, sp, and RgoogleMaps make the
be automatically generated),
process much easier; in fact, RgoogleMaps was the
inspiration for ggmap. Moreover there has recently a coordinate system, and
been a deluge of interest in the subject of mapmaking
in RIan Fellows excellent interactive GUI-driven a facet specification.
DeducerSpatial package based on Bing Maps comes Since ggplot2 is an implementation of the layered
to mind. ggmap takes another step in this direction grammar of graphics, every plot made with gg-
by situating the contextual information of various plot2 has each of the above elements. Consequently,
kinds of static maps in the ggplot2 plotting frame- ggmap plots also have these elements, but certain el-
work. The result is an easy, consistent way of spec- ements are fixed to map components : the x aesthetic
ifying plots which are readily interpretable by both is fixed to longitude, the y aesthetic is fixed to lati-
expert and audience and safeguarded from graphi- tude, and the coordinate system is fixed to the Mer-
cal inconsistencies by the layered grammar of graph- cator projection.1
ics framework. The result is a spatial plot resembling The major theoretical advantage of using the lay-
Figure 3. ered grammar in plotting maps is that aesthetic
scales are kept consistent. In the typical situation
where the map covers the extent of the data, in
murder <- subset(crime, offense == "murder")
ggmap the latitude and longitude scales key off the
map (by default) and one scale is used for those
qmplot(lon, lat, data = murder,
axes. The same is true of colors, fills, alpha blend-
colour = I('red'), size = I(3), darken = .3)
ings, and other aesthetics which are built on top of
the map when other layers are presentedeach is al-
lotted one scale which is kept consistent across each
layer of the plot. This aspect of the grammar is par-
ticularly important for faceted plots in order to make
a proper comparison across several plots. Of course,
the scales can still be tricked if the user improperly
specifies the spatial data, e.g. using more than one
projection in the same map, but fixing such errors is
beyond any framework.
The practical advantage of using the grammar is
even better. Since the graphics are done in ggplot2
the user can draw from the full range of ggplot2s
capabilities to layer elegant visual contentgeoms,
stats, scales, etc.using the usual ggplot2 coding
conventions. This was already seen briefly in Figure
3 where the arguments of qmplot are identical to that
of ggplot2s qplot; much more will be seen shortly.
plot2, and then plot additional content layers of data, necessary) and passing them to each of the API spe-
statistics, or models on top of the map. In ggmap this cific get_* functions. To ensure that the resulting
process is broken into two pieces (1) downloading maps are the same across the various sources for
the images and formatting them for plotting, done the same location/zoom specification, get_map first
with get_map, and (2) making the plot, done with grabs the appropriate Google Map, determines its
ggmap. qmap marries these two functions for quick bounding box, and then downloads the other map as
map plotting (c.f. ggplot2s ggplot), and qmplot at- needed. In the case of Stamen Maps and Cloudmade
tempts to wrap up the entire plotting process into Maps, this involves a stitching process of combining
one simple command (c.f. ggplot2s qplot). several tiles (small map images) and then cropping
the result to the appropriate bounding box. The re-
sult is a single, consistent specification syntax across
The get_map function
the four map sources as seen for Google Maps and
In ggmap, downloading a map as an image and OpenStreetMap in Figure 4.
formatting the image for plotting is done with the baylor <- "baylor university"
get_map function. More specifically, get_map is qmap(baylor, zoom = 14)
a wrapper function for the underlying functions
get_googlemap, get_openstreetmap, get_stamenmap,
and get_cloudmademap which accepts a wide array
of arguments and returns a classed raster object for
plotting with ggmap.
As the most important characteristic of any map
is location, the most important argument of get_map
is the location argument. Ideally, location is a lon-
gitude/latitude pair specifying the center of the map
and accompanied by a zoom argument, an integer
from 3 to 20 specifying how large the spatial extent
should be around the center, with 3 being the conti-
nent level and 20 being roughly the single building
level. location is defaulted to downtown Houston,
Texas, and zoom to 10, roughly a city-scale.
While longitude/latitude pairs are ideal for spec-
ifying a location, they are somewhat inconvenient on
a practical level. For this reason, location also ac-
cepts a character string. The string, whether con-
taining an address, zip code, or proper name, is
then passed to the geocode function which then de- qmap(baylor, zoom = 14, source = "osm")
termines the appropriate longitude/latitude coordi-
nate for the center. In other words, there is no need
to know the exact longitude/latitude coordinates of
the center of the mapget_map can determine them
from more colloquial ("lazy") specifications so that
they can be specified very loosely. For example, since
Before moving into the source and maptype argu- other hand, only provides the default style shown in
ments, it is important to note that the underlying API Figure 4.
specific get_* functions for which get_map is a wrap- Style is where Stamen Maps and Cloudmade
per provide more extensive mechanisms for down- Maps really shine. Stamen Maps has three available
loading from their respective sources. For example, tile setsterrain (e.g. Figures 3 or 16), watercolor,
get_googlemap can access almost the full range of the and toner.
Google Static Maps API as seen in Figure 5. qmap(baylor, zoom = 14, source = "stamen",
maptype = "watercolor")
set.seed(500)
df <- round(data.frame(
x = jitter(rep(-95.36, 50), amount = .3),
y = jitter(rep( 29.76, 50), amount = .3)
), digits = 2)
Google tile sets. The latter, for example, is ideal for qmap("houston", zoom = 10, maptype = 58916,
black-and-white plotting. api_key = api_key, source = "cloudmade")
Both Stamen Maps and CloudMade Maps are Figure 7: Two out of thousands of user made Cloud-
built using OpenStreetMap data. These data are Made Maps styles. The top is comparable to Figures
contributed by an open community of online users 4 and 6, and the bottom is the bodies of water in Fig-
in much the same way Wikipedia isboth are free, ure 5
both are user-contributed, and both are easily edited.
Moreover, OpenStreetMap has data not only on The ggmap function
roadways and bodies of water but also individual Once get_map has grabbed the map of interest, ggmap
buildings, fountains, stop signs and other apparent is ready to plot it. The result of get_map is a specially
minutiae. The draw back is that (like Google Maps) classed raster object (a matrix of colors as hexadeci-
not all locations are mapped with the same degree of mal character strings)
precision. For example, entire lakes might be missing > paris <- get_map(location = "paris")
from the tile sets. > str(paris)
chr [1:1280, 1:1280] "#C6DAB6" "#C2D6B3" "#C2D6B3" ...
- attr(*, "class")= chr [1:2] "ggmap" "raster"
qmap(baylor, zoom = 14, maptype = 53428, - attr(*, "bb")='data.frame': 1 obs. of 4 variables:
api_key = api_key, source = "cloudmade") ..$ ll.lat: num 48.6
..$ ll.lon: num 1.91
..$ ur.lat: num 49.1
..$ ur.lon: num 2.79
ranges specified in the bb attribute of the ggmap ob- termines where a legend should be drawn on the
ject. Thus, the default base layer of the ggplot2 object map if one should be drawn. Its options are "left",
created by ggmap is ggplot(aes(x=lon,y=lat),data "right" (default), "bottom", "top", "topleft",
= fourCorners), and the default x and y aesthetic "bottomleft", "topright", "bottomright" and
scales are calculated based on the longitude and lati- "none". The first four draw the legend according to
tude ranges of the map. ggplot2s normal specifications (without any axes);
the next four draw the legend on top of the map itself
similar to ArcGIS; and the last eliminates the legend
49.1
altogether. padding governs how far from the corner
the legend should be drawn when on top of the map.
The darken argument, a suggestion by Jean-
49.0
Olivier Irisson, tints the map image. The default,
c(0,"black"), indicates a fully translucent black
layer, i.e. no tint at all. Generally, the first argument
48.9 corresponds to an alpha blending (0 = invisible, 1 =
opaque) and the second argument the color of the
lat
not only for ggmap plots, but ggplot2 graphics in
general.
> # find a reasonable spatial extent
> gglocator(2)
lon lat
1 -95.39681 29.78400
2 -95.34188 29.73631
# only violent crimes
violent_crimes <- subset(crime,
offense != "auto theft" &
offense != "theft" & offense != "burglary")
theme_set(theme_bw(16))
HoustonMap <- qmap("houston", zoom = 14,
color = "bw", legend = "topleft")
HoustonMap +
geom_point(aes(x = lon, y = lat,
colour = offense, size = offense), Figure 10: Contour plot of violent crimes by offense
data = violent_crimes) type
Figure 10 is the first time we really begin to see overlay <- stat_density2d(
the power of having the maps in the ggplot2 frame- aes(x = lon, y = lat, fill = ..level..,
work. While it is actually not a very good plot, alpha = ..level..),
it illustrates the practical advantage of the ggplot2 bins = 4, geom = "polygon",
framework with the contextual information of the data = violent_crimes)
mapsplitting the data frame violent_crimes into
chunks based on the offense variable, binning the HoustonMap + overlay + inset(
points of each, and aggregating back into one data set grob = ggplotGrob(ggplot() +
to plot is all done entirely behind the scenes by gg- overlay + theme_inset()),
plot2. ggplot2 has several such canned procedures. xmin = -95.35836, xmax = Inf,
What about violent crime in general? If we ne- ymin = -Inf, ymax = 29.75062
glect the type of offense, we can get a good idea of the )
spatial distribution of violent crimes by using a con-
tour plot. Since the map image itself is based on gg- Violent
Crime
plot2s new annotation_raster, which doesnt have Density
a fill aesthetic, we can access the fill aesthetic to make 1400
a filled contour plot. 1200
1000
houston <- get_map('houston", zoom = 14)
800
HoustonMap <- ggmap("houston",
600
extent = "device", legend = "topleft")
400
HoustonMap +
stat_density2d(
aes(x = lon, y = lat, fill = ..level..,
alpha = ..level..),
size = 2, bins = 4, data = violent_crimes,
geom = "polygon")
Violent
Crime
Density
1400
1200
fill = ..level.., alpha = ..level..), = "simple", only longitudes and latitudes are re-
bins = 5, geom = "polygon", turned. These are actually Mercator projections of
data = violent_crimes) + the ubiquitous unprojected 1984 world geodetic sys-
scale_fill_gradient(low = "black", tem (WGS84), a spheroidal earth model used by
high= "red") + Google Maps. When output is set to "more", a larger
facet_wrap(~ day) data frame is returned which provides much more
Google Geocoding information on the query :
Monday Tuesday Wednesday
29.78
> geocode("baylor university", output = "more")
29.77
lon lat type loctype
29.76
1 -97.11441 31.54872 university approximate
29.75
address north south east
29.74
1 [long address] 31.55823 31.53921 -97.0984
Violent
Thursday Friday Saturday Crime west postal_code country
29.78 Density
1 -97.13042 76706 united states
29.77 1500
administrative_area_level_2
Latitude
29.76
1000 1 mclennan
29.75
administrative_area_level_1 locality street
29.74 500 1 texas waco s 5th st
Sunday
29.78 streetNo point_of_interest
29.77 1 1311 <NA>
29.76
In particular, administrative bodies at various levels
29.75
are reported. Going further, setting output = "all"
29.74
95.39 95.37 95.35 returns the entire JavaScript Object Notation (JSON)
Longitude
tree given by the Google Geocoding API parsed by
Figure 13: Faceted filled contour plot by day rjson (Couture-Beil, 2011).
The Geocoding API has a number of request lim-
This last plot displays one of the known issues itations in place to prevent abuse. An unspecified
with contour plots in ggplot2a "clipping" or "tear- short-term rate limit is in place (see mapdist below)
ing" of the contours. Aside from that fact (which will as well as a 24-hour limit of 2,500 requests. These are
likely be fixed in subsequent ggplot2 versions), we monitored to some extent by the hidden global vari-
can see that in fact most violent crimes happen on able .GoogleGeocodeQueryCount and exported func-
Monday, with a distant second being Friday. Fridays tion geocodeQueryCheck. geocode uses these to mon-
pattern is easily recognizablea small elevated dot itor its own progress and will either (1) slow its rate
in the downtown bar district and an expanded re- depending on usage or (2) error if the query limit is
gion to the southwest in the district known as mid- exceeded. Note that revgeocode shares the same re-
town, which has an active nightlife. Mondays pat- quest pool and is monitored by the same variable and
tern is not as easily explainableit may be that more function. To learn more about the Google Geocod-
jail inmates are released on Monday who had week- ing, Distance Matrix, and Directions API usage regu-
end stays. lations, see the websites listed in the bibliography.
revgeocode
ggmaps utility functions
In some instances it is useful to convert longi-
ggmap has several utility functions which aid in spa- tude/latitude coordinates into a physical address.
tial exploratory data analysis. This is made possible (to the extent to which it is pos-
sible) with the revgeocode function which also relies
geocode on the Google Geocoding API.
The ability to move from an address to a longi- > gc <- geocode("baylor university")
tude/latitude coordinate is virtually a must for vi- > (gc <- as.numeric(gc))
sualizing spatial data. Unfortunately however, the [1] -97.11441 31.54872
process is almost always done outside R by using a > revgeocode(gc)
proper geographic information system (GIS), saving [1] "S 1st St, Baylor University, Waco, TX
the results, and importing them into R. The geocode 76706, USA"
function simplifies this process to a single line in R. Like geocode, more output can be provided as well
geocode is a vectorized function which accepts
character strings and returns a data frame of geo- > revgeocode(gc, output = "more")
graphic information. In the default case of output address route establishment
Route A B C
A B C
31.558
31.556
31.554
Latitude
31.552
31.550
31.548
Figure 16: Plotting shape files Census tracts in Texas from the 2000 U.S. Census