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
A good example of this is
With that in mind, I want to quickly show you how to use
First, let’s just load the packages that we will use. Here, we’re going to load
#============== # LOAD PACKAGES #============== library(ggmap) library(tidyverse)
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 #================= 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
map.tokyo. 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 tidyversepackage.)
#============================================= # USE THE PIPE OPERATOR # - 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
Now let’s plot a different map. Here we will plot a map of Japan.
#========================================== # GET 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
Having said that, this plot is not very good, because it’s not really zoomed properly.
To fix this, we will use the
#=========================================== # GET MAP OF JAPAN, ZOOMED # - 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
#============================== # GET MAP OF SPECIFIC LOCATION #============================== # 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
To be clear, there’s actually quite a bit more functionality for
# CREATE DATA FRAME df.tokyo_locations % 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
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.
SIGN UP NOW
R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...