Select Page

When you’re learning data science – and specifically data science in R using the Tidyverse – it is very helpful to learn individual techniques in a highly modular way.

This is because large projects, when you really break them down, are just combinations of small techniques. If you master all of those little tools and techniques on a small scale then you can combine them together later to create more complex charts, graphs, and analyses.

At a very basic level, this means that you should master fundamental tools like geom_bar(), geom_line(), and geom_point(). After you master those very simple tools, you can learn additional tools that can be combined with the basics.

A good example of this is ggmap. The ggmap package allows you to download and plot basic maps from Google maps (and a few other sources). These maps can then be used as layers within the ggplot2 plotting system. Essentially, you can plot maps from ggmap, and then use ggplot2 to plot points and other geoms on top of the map. Using ggplot+ggmap this way is extremely useful for quick-and-dirty geospatial visualization.

With that in mind, I want to quickly show you how to use ggmap to download and plot maps.

First, let’s just load the packages that we will use. Here, we’re going to load ggmap and tidyverse. ggmap will allow us to access Google Maps, but we’ll also use tidyverse for some additional plotting and other functionality (like pipes).



Now that our packages are downloaded, let’s just get a simple map. Here, we’ll retrieve a basic map of Tokyo from Google Maps.

# GET MAP OF Tokyo
#================= <- get_map("Tokyo")

Notice that we’ve used two functions.

First we used get_map() to retrieve the map from Google Maps. To do this, we just specified the name of the location (more on that later).

Next, we used ggmap() to plot the map.

In doing this, I saved the map first with the name It can be useful sometimes to save a map like this with a name, but sometimes you don’t need the name. In fact, if you don’t need to save the map, then it can be useful to avoid doing so; finding names for little objects like this can become tiresome.

Having said that, we can actually retrieve and plot the map in a single line of code, without saving the map object.

To do this, we will use the tidyverse “pipe” operator, %>%. (Note: this is one of the reasons that we loaded the tidyverse package.)

# - Here, we're basically doing the same thing
#   but we will do it in one line of code

get_map("Tokyo") %>% ggmap()

Essentially, what we’ve done here is retrieved the map using get_map(), but then immediately “piped” it into ggmap(). Again, this can be useful because the code is cleaner … we don’t need an intermediate name.

Now let’s plot a different map. Here we will plot a map of Japan.

# - this doens't work well without zooming

get_map("Japan") %>% ggmap()

This code works in a similar way: we just provide the location name to get_map(), and then pipe it into ggmap().

Having said that, this plot is not very good, because it’s not really zoomed properly.

To fix this, we will use the zoom parameter of get_map() to zoom out.

# - here, we are manually setting the zoom
#   to get a better map
# - to find the best setting for 'zoom', 
#   you'll need to use some trial-and-error

get_map("Japan", zoom = 5) %>% ggmap()

This is much better.

Keep in mind that to get the right setting for zoom, you’ll typically need to use a bit of trial-and-error.

Next, we’ll get a map of a more specific location. Here we will get a map of Shinjuku, an area of Tokyo.

Notice that as we do this, we are again just specifying the location and zooming in properly with the zoom parameter.


# note: not zoomed enough
get_map("Shinjuku") %>% ggmap()

# this is properly zoomed
get_map("Shinjuku", zoom = 16) %>% ggmap()

So there you have it. That’s the quick introduction to ggmap.

To be clear, there’s actually quite a bit more functionality for get_map(), but in the interest of simplicity, you should master these techniques first. Later, once you have mastered these simple ggmap tools, you can start combining maps from ggmap() with tools from ggplot2:

df.tokyo_locations <- tibble(location = c("Ueno, Tokyo, Japan"
                                          ,"Shibuya, Tokyo, Japan"
                                          ,"Shinjuku, Tokyo, Japan"))

geo.tokyo_locations <- geocode(df.tokyo_locations$location)

df.tokyo_locations <- cbind(df.tokyo_locations, geo.tokyo_locations)

get_map("Tokyo", zoom = 12) %>% ggmap() +
  geom_point(data = df.tokyo_locations, aes(x = lon, y = lat), color = 'red', size = 3)

By the way, this is exactly why I recommend learning and mastering simple tools like geom_point() first … once you have mastered simple tools, you can start combining them together like building-blocks to create more advanced visualizations and analyses.

Sign up to master R

Want to master R?

Here at Sharp Sight, we teach data science in R.

And we not only show you the techniques, we will show you how to practice those techniques so that you master them.

So if you want to master data science and become “fluent” in R, sign up for our email newsletter.

When you sign up, you’ll get:
– ggplot2 data visualization tutorials
– tutorials on how to practice ggplot2 syntax (so you can write code “with your eyes closed”)
– practice tips to help you master and memorize syntax

You’ll also get access to our “Data Science Crash Course” for free.