Maps with ggmap

Ggplot2 is a powerful and awesome tool to visualize data and statistical analysis in R. Today I’d like to show you how to plot spatial data over static maps from Google Maps, OpenStreetMap, Stamen Maps, or CloudMade Maps. Ggmap which allows us to visualize any part of the world with a simple code. This function download coordinates with get_map() and then plot the map. qmplot() is the ggmap equivalent to qplot() for quick plotting maps and qmap() is a wraper for ggmap() and get_map().

In the example below we’re going to represent ecuador earthquakes from April 23. Data can be downloaded from http://earthquake.usgs.gov/earthquakes/feed/v1.0/csv.php.

library(ggplot2)
library(ggmap)
EQ <- read.csv("all_day.csv")
str(EQ)
EQ <- EQ[grepl("Ecuador", EQ$place),]
EcuadorMap <- qmap("ecuador", zoom=6)
EcuadorMap +
geom_point(aes(x=longitude, y=latitude, size=mag),
data=EQ, color="#ff7f00", alpha=0.7) +
labs(title="Ecuador Earthquakes",x="Longitude",y="Latitude",size="Magnitude")

 

Rplot.png

For more information and examples see https://journal.r-project.org/archive/2013-1/kahle-wickham.pdf

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