Tutorials

A Simple Tutorial of the ggmap package in R

The R package ggmap allows ggplot2 capabilities with google maps. I have recently used it for a few projects, and wanted to save a few quick sample plots so I can easily return to creating similar plots. Here are a few examples of ggmap plots.

The first example uses the ‘crime’ dataset that comes in the ggmap package. This dataset shows crimes in the Houston area.

map211

require(ggmap)
require(ggplot2)

houston <- get_map(location = 'Houston', zoom = 10, maptype = "roadmap")

ggmap(houston) +
geom_point(data = crime[crime$offense == "murder",],
mapping = aes(x = lon, y = lat),
color="hotpink", shape=3, size=3) +
ggtitle("ggmap example using\nthe crime R dataset") +
theme(plot.title = element_text(hjust = 0.5))

This second plot is an example using the satellite plot type. This plot shows eathquakes near Fiji. The data is from the ‘quakes’ dataset in the R package ‘datasets’.

Map1

require(datasets)
require(ggmap)
require(ggplot2)

fiji <- get_map(location = c(mean(quakes$long), mean(quakes$lat)), zoom = 5,
maptype = "satellite")

ggmap(fiji) +
geom_point(data = quakes, mapping = aes(x = long, y = lat, color = depth, size=mag)) +
scale_colour_gradient(low = "hotpink",high = "yellow") +
ggtitle("ggmap example using\nthe Fiji earthquakes R dataset ") +
theme(plot.title = element_text(hjust = 0.5))

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s