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:
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:
If we want more information, say the detailed address, we can add the option of
output = more:
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:
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.
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.
source: There four sources for
get_map() to look for, “google”, “osm”, “stamen”, “cloudmade”. Equivalently, The function
get_map() consists of four functions:
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.
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
But this option can change the base layer to whatever you want.
Here we use the data of earthquake in China to illustrate.
This is the end of this series.