0% found this document useful (0 votes)
819 views2 pages

GG Map Cheat Sheet

This document provides instructions for creating maps using the ggmap package in R. There are two main steps: 1) downloading a map raster from sources like Stamen, Google, or OpenStreetMap by defining a location or bounding box and specifying the map type and color, and 2) plotting the raster and overlaying additional data layers like points or polygons. Fine-tuning options include adjusting the zoom level, adding legends and annotations, and controlling aesthetics like the size, color, transparency and scaling of overlaid points and shapes.

Uploaded by

Harshini Mohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
819 views2 pages

GG Map Cheat Sheet

This document provides instructions for creating maps using the ggmap package in R. There are two main steps: 1) downloading a map raster from sources like Stamen, Google, or OpenStreetMap by defining a location or bounding box and specifying the map type and color, and 2) plotting the raster and overlaying additional data layers like points or polygons. Fine-tuning options include adjusting the zoom level, adding legends and annotations, and controlling aesthetics like the size, color, transparency and scaling of overlaid points and shapes.

Uploaded by

Harshini Mohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

ggmap quickstart

For more functionality, see ggmap documentation and


https://dl.dropboxusercontent.com/u/24648660/ggmap%20u
seR%202012.pdf

The following maps show different map source/type options (except cloudmade)
The appearance of these maps may be very different depending on zoom/scale

stamen: watercolor

stamen: toner

stamen: terrain

google: terrain

google: satellite

google: roadmap

There are 2 basic steps to making a map using ggmap:


Part 1: Download
map raster

Part 2: Plot raster


and overlay data

Part 1: Downloading the map raster


Start by loading the package: library(ggmap)
1. Define location: 3 ways
- location/address
myLocation <- "University of Washington
- lat/long
myLocation <- c(lon = -95.3632715, lat = 29.7632836)
- bounding box lowerleftlon, lowerleftlat, upperrightlon, upperrightlat
(a little glitchy for google maps)
myLocation <- c(-130, 30, -105, 50)

If you cant get the


map you want by
adjusting the
location/zoom
variables, the
functions designed
for the different map
sources provide more
options:
get_googlemap,
get_openstreetmap,
get_stamenmap,
get_cloudmademap

google: hybrid

Convert location/address its lat/long coordinates:


geocode(University of Washington)
2. Define map source, type, and color
The get_map function provides a general approach for quickly
obtaining maps from multiple sources. I like this option for exploring
different styles of maps.
There are 4 map sources to obtain a map raster, and each of these
sources has multiple map types (displayed on right).
- stamen: maptype = c(terrain, toner, watercolor)
- google: maptype = c(roadmap, terrain, satellite, hybrid)
- osm: open street map
- cloudmade: 1000s of maps, but an api key must be obtained
from http://cloudmade.com

osm*
All maps can be displayed
in black and white
color = bw

myMap <- get_map(location=myLocation,


source="stamen", maptype=watercolor", crop=FALSE)
ggmap(myMap)
This will produce a map that looks something like this
NOTE: crop = FALSE because otherwise, with stamen plots, the map is
slightly shifted when I overlay data.
3.

Fine tune the scale of the map using zoom


The get_map function takes a guess at the zoom level, but
you can alter it:
zoom = integer from 3-21
3 = continent, 10=city, 21=building
(openstreetmap limit of 18)

*Open street maps may return


a '503 Service Unavailable
error. This means their server
is unavailable, and you must
wait for it to become available
again.

myMap <get_map(location=myLocation,
source=osm", color=bw))

If you use Rstudio:


Sometimes a plot will not
display. Increasing the size
of the plot window may
help. dev.off() prior to
plotting may also help.
The urlonly = TRUE will
return a url that will
display your map in a web
browser. Which is pretty
cool and may be handy!
legend=topleft will inset
the legend on the top left
of the map is data is
overlayed (page 2).

Page 1 ggmap,
Melanie Frazier

ggmap quickstart
Part 2: Plotting the maps and data
1 . Plot the raster:
ggmap(myMap)
2 . Add points with latitude/longitude coordinates:
ggmap(myMap)+
geom_point(aes(x = Longitude, y = Latitude), data = data,
alpha = .5, color="darkred", size = 3)
alpha = transparency
color = color
size = size of points

3. Add polygons from shp file


The shp file is imported into R using the rgdal package, and must be transformed to geographic
coordinates (latitude/longitude) on the World Geodetic System of 1984 (WGS84) datum using
the rgdal package:
library(rgdal)
shpData <- readOGR(dsn="C:\\Documents and Settings\\Watershed", layer="WS")
proj4string(shpData) # describes datas current coordinate reference system
# to change to correct projection:
shpData <- spTransform(shpData,
CRS("+proj=longlat +datum=WGS84"))
To plot the data:
geom_polygon(aes(x = long, y = lat, group=id),
data = shpData, color ="white", fill ="orangered4",
alpha = .4, size = .2)
color= outline color

alpha = transparency

The size, color, alpha, and shape of the points can be scaled relative
fill = polygon color
size = outline size
to another variable (in this case estArea) within the aes function:
ggmap(myMap)+
4. Annotate figure
geom_point(aes(x = Longitude, y = Latitude, size=sqrt(estArea)),
baylor <- get_map('baylor university', zoom = 15, maptype = 'satellite')
data = data, alpha = .5, color="darkred")
ggmap(baylor) +
annotate('rect', xmin=-97.11, ymin=31.54, xmax=-97.12, ymax=31.55, col="red, fill=white)+
annotate('text', x=-97.12, y=31.54, label = 'Statistical Sciences', colour = I(red'), size = 8)+
annotate('segment', x=-97.12, xend=-97.12, y=31.55, yend=31.55,
colour=I('red'), arrow = arrow(length=unit(0.3,"cm")), size = 1.5) +
labs(x = 'Longitude', y = 'Latitude') + ggtitle('Baylor University')

Controlling size and color


size

color
Additional functions can be added to control scaling, e.g.:
ggmap(myMap)+
geom_point(aes(x = Longitude, y = Latitude, size=sqrt(estArea)),
data = data, alpha = .5, color="darkred")+
scale_size(range=c(3,20))

scale_size(range = c(3, 20))


But, the following is better for display because
it is based on area (rather than radius)
scale_area(range=c(3,20))
Continuous variables: color gradient between
n colors, e.g.:
scale_colour_gradientn(colours =
rainbow_hcl(7))
Discrete variables, e.g.:
scale_colour_manual(values=rainbow_hcl(7))
scale_colour_manual(values=c("8" = "red",
"4" = "blue","6" = "green)
*Use colorspace and RColorBrewer to choose
color combinations
Page 2 ggmap,
Melanie Frazier

You might also like