Zhen Zhang's Blog

R map part 3 -- ggmap

Now here we will discuss a more fantastic package: ggmap. ggmap is a map implementation of ggplot. With this tool, we can use google map through its API. In this post, I will first introduce some small functions, then comes to our main character: get_map() and ggmap().

Small Tools

geocode()

With this function, I can generate the coordinates of a particular place. For instance, I want the longitude and latitude of Central University of Finance and Economics:

1
2
3
4
library(ggmap)
cufe <- geocode("central university of finance and economics")
cufe
##

If we want more information, say the detailed address, we can add the option of output = more:

1
2
geocode("central university of finance and economics", output = more)
##

revgeocode()

What if we know the coordinates of a place, but wants to know its name, also known as the reverse behavior of our previous function? The revgeocode() can help us:

1
2
revgeocode(cufe)
##

mapdist()

With this function, we can easily calculate the distance between two places. Also, we can set the mode to “driving”, “walking” or “bicycling” to show the amount of time needed to do so.

1
2
3
mapdist("tianjin", "beijing", mode = "driving")
## from to m km miles seconds minutes hours
## 1 tianjin beijing 146159 146.159 90.8232 7333 122.2167 2.036944

get_map()

The function get_map() returns an raster object which can be used by ggmap to plot the map.

There are many important options:

location: the center of the map. The default value is the city of Houston of Texas. It can receive string (like “beijing”) or coordinates as its arguments.

zoom: It represents the limit the center of city expands. The limit of this value is 1 to 21, where 1 is the level of continent (largest), and 21 is the level of building (smallest). Usually for the whole city, 12 is all right.

1
2
beijing <- get_googlemap("beijing", zoom = 12)
ggmap(beijing)

beijing_map

source: There four sources for get_map() to look for, “google”, “osm”, “stamen”, “cloudmade”. Equivalently, The function get_map() consists of four functions: get_googlemap(), get_openstreetmap(), get_stamenmap(), get_cloudmademap(). When we call get_map(), it calls one of the four functions as we request.

maptype: It differs for different kinds of map source. Google Map includes “terrain”, “terrain-background”, “satellite”, “roadmap”, and “hybrid”. Stamen Map includes “terrain”, “watercolor”, and “toner”. Cloudmade Maps is much more complicated, and I will not discuss it here.

1
ggmap(get_map("shanghai", zoom = 12, source = "stamen",maptype = "watercolor"))

shanghai_watercolor_map

ggmap()

ggmap receives a raster object as its inputs (usually generated by get_map()) and then plot the corresponding map. Some of its options include:

legend & padding: they are related to legend. The former one can set the positions, for instance, “left”, “right” and so on. The latter one set the distance between the legend and the corner.

extent: it controls how much of the plot should the map take up. The options are “normal”, “device” or “panel” (default)

base_layer: In default, ggmap is equivalent to

1
2
3
4
ggplot(aes(x=lon, y=lat), data=fourCorners) +
geom_blank() +
coord_map("mercator") +
annotation_raster(ggmap, xmin, xmax, ymin, ymax)

But this option can change the base layer to whatever you want.

Application

Here we use the data of earthquake in China to illustrate.

1
2
3
4
5
6
7
8
9
10
11
12
13
library(XML)
webpage <-'http://data.earthquake.cn/datashare/globeEarthquake_csn.html'
tables <- readHTMLTable(webpage,stringsAsFactors = FALSE) ## parse the html to XML object
raw <- tables[[6]] ## see where the data is located
data <- raw[-1,c('V1','V3','V4')] ## extract the time and coordinates of the data
names(data) <- c('date','lan','lon') ## make it understandable
data$lan <- as.numeric(data$lan)
data$lon <- as.numeric(data$lon)
data$date <- as.Date(data$date, "%Y-%m-%d") ## adjust the data format for the plot
ggmap(get_googlemap(center = 'china', zoom = 4, maptype = 'terrain'), extent = 'device') +
geom_point(data = data, aes(x = lon, y = lan), color = 'red', alpha = 0.7) +
stat_density2d(aes(x = lon, y = lan, fill = ..level.., alpha = ..level..), size = 2, bins = 4, data = data, geom = 'polygon') +
theme(legend.position = "none")

earthquake

This is the end of this series.

References

  1. Douban R by shilululululu
  2. 用R软件绘制中国分省市地图 by 邱怡轩
  3. 用ggmap包进行地震数据的可视化 by 肖凯
  4. R绘制中国地图,并展示流行病学数据 by 姜晓东