<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/how-to-plot-basic-maps-with-ggmap/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://sharpsightlabs.com/blog/basic-maps-ggmap/"> r-bloggers – SHARP SIGHT LABS</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>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.</p>
<p>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.</p>
<p>At a very basic level, this means that you should master fundamental tools like <a href="http://sharpsightlabs.com/blog/r-bar-chart-basic/"></a>, <a href="http://sharpsightlabs.com/blog/line-chart-ggplot2-amzn/"></a>, and . After you master those very simple tools, you can learn additional tools that can be combined with the basics.</p>
<p>A good example of this is . The <a href="http://stat405.had.co.nz/ggmap.pdf"></a> 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  plotting system. Essentially, you can plot maps from , and then use  to plot points and other geoms <em>on top of the map</em>. Using + this way is extremely useful for quick-and-dirty <a href="https://www.r-bloggers.com/how-to-plot-basic-maps-with-ggmap/sharpsightlabs.com/blog/mapping-paris-bike-stands/">geospatial visualization</a>.</p>
<p>With that in mind, I want to quickly show you how to use  to download and plot maps.</p>
<p>First, let’s just load the packages that we will use. Here, we’re going to load  and .  will allow us to access Google Maps, but we’ll also use  for some additional plotting and other functionality (like pipes).</p>
<pre class="wp-code-highlight">
#==============
# LOAD PACKAGES
#==============

library(ggmap)
library(tidyverse)

</pre>
<p>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.</p>
<pre class="wp-code-highlight">
#=================
# GET MAP OF Tokyo
#=================

map.tokyo &lt;- get_map(&quot;Tokyo&quot;)
ggmap(map.tokyo)

</pre>
<p><a rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-ggplot2/" target="_blank"><br>
<img sizes="(max-width: 600px) 100vw, 600px" src="https://i2.wp.com/sharpsightlabs.com/wp-content/uploads/2017/11/ggmap_tokyo-600x569.png?w=450" alt="" class="wp-image-3155" data-recalc-dims="1"><br>
</a></p>
<p>Notice that we’ve used two functions. </p>
<p>First we used  to retrieve the map from Google Maps. To do this, we just specified the name of the location (more on that later).</p>
<p>Next, we used  to plot the map.</p>
<p>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 <em>don’t</em> need to save the map, then it can be useful to avoid doing so; finding names for little objects like this can become tiresome.</p>
<p>Having said that, we can actually retrieve and plot the map in a single line of code, without saving the map object.</p>
<p>To do this, we will use the tidyverse “pipe” operator, . (Note: this is one of the reasons that we loaded the  package.)</p>
<pre class="wp-code-highlight">
#=============================================
# USE THE PIPE OPERATOR
# - Here, we're basically doing the same thing
#   but we will do it in one line of code
#=============================================

get_map(&quot;Tokyo&quot;) %>% ggmap()

</pre>
<p><a rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-ggplot2/" target="_blank"><br>
<img sizes="(max-width: 600px) 100vw, 600px" src="https://i2.wp.com/sharpsightlabs.com/wp-content/uploads/2017/11/ggmap_tokyo-600x569.png?w=450" alt="" class="wp-image-3155" data-recalc-dims="1"><br>
</a></p>
<p>Essentially, what we’ve done here is retrieved the map using , but then immediately “piped” it into . Again, this can be useful because the code is cleaner … we don’t need an intermediate name.</p>
<p>Now let’s plot a different map. Here we will plot a map of Japan.</p>
<pre class="wp-code-highlight">
#==========================================
# GET MAP OF JAPAN
# - this doens't work well without zooming
#==========================================

get_map(&quot;Japan&quot;) %>% ggmap()

</pre>
<p><a rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-ggplot2/" target="_blank"><br>
<img sizes="(max-width: 600px) 100vw, 600px" src="https://i0.wp.com/sharpsightlabs.com/wp-content/uploads/2017/11/ggmap_japan_nozoom-600x569.png?w=450" alt="" class="wp-image-3156" data-recalc-dims="1"><br>
</a></p>
<p>This code works in a similar way: we just provide the location name to , and then pipe it into .</p>
<p>Having said that, this plot is not very good, because it’s not really zoomed properly.</p>
<p>To fix this, we will use the  parameter of  to zoom out.</p>
<pre class="wp-code-highlight">
#===========================================
# 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(&quot;Japan&quot;, zoom = 5) %>% ggmap()

</pre>
<p><a rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-ggplot2/" target="_blank"><br>
<img sizes="(max-width: 600px) 100vw, 600px" src="https://i2.wp.com/sharpsightlabs.com/wp-content/uploads/2017/11/ggmap_japan_withzoom-600x591.png?w=450" alt="" class="wp-image-3157" data-recalc-dims="1"><br>
</a></p>
<p>This is much better.</p>
<p>Keep in mind that to get the right setting for zoom, you’ll typically need to use a bit of trial-and-error.</p>
<p>Next, we’ll get a map of a more specific location. Here we will get a map of Shinjuku, an area of Tokyo.</p>
<p>Notice that as we do this, we are again just specifying the location and zooming in properly with the  parameter.</p>
<pre class="wp-code-highlight">
#==============================
# GET MAP OF SPECIFIC LOCATION
#==============================

# note: not zoomed enough
get_map(&quot;Shinjuku&quot;) %>% ggmap()


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

</pre>
<p><a rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-ggplot2/" target="_blank"><br>
<img sizes="(max-width: 600px) 100vw, 600px" src="https://i2.wp.com/sharpsightlabs.com/wp-content/uploads/2017/11/ggmap_shinjuku-600x563.png?w=450" alt="" class="wp-image-3158" data-recalc-dims="1"><br>
</a></p>
<p>So there you have it. That’s the quick introduction to .</p>
<p>To be clear, there’s actually quite a bit more functionality for , but in the interest of simplicity, you should master these techniques first. Later, once you have mastered these simple  tools, you can start combining maps from  with tools from :</p>
<pre class="wp-code-highlight">
# CREATE DATA FRAME
df.tokyo_locations &lt;- tibble(location = c(&quot;Ueno, Tokyo, Japan&quot;
                                          ,&quot;Shibuya, Tokyo, Japan&quot;
                                          ,&quot;Shinjuku, Tokyo, Japan&quot;))


# GEOCODE
geo.tokyo_locations &lt;- geocode(df.tokyo_locations$location)


# COMBINE DATA
df.tokyo_locations &lt;- cbind(df.tokyo_locations, geo.tokyo_locations)


# USE WITH GGPLOT
get_map(&quot;Tokyo&quot;, zoom = 12) %>% ggmap() +
  geom_point(data = df.tokyo_locations, aes(x = lon, y = lat), color = 'red', size = 3)
</pre>
<p><a rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-ggplot2/" target="_blank"><br>
<img sizes="(max-width: 600px) 100vw, 600px" src="https://i2.wp.com/sharpsightlabs.com/wp-content/uploads/2017/11/ggmap_with_ggplot_simple_example-600x544.png?w=450" alt="" class="wp-image-3159" data-recalc-dims="1"><br>
</a></p>
<p>By the way, this is exactly why I recommend learning and mastering simple tools like  first … once you have mastered simple tools, you can start combining them together like building-blocks to create <a href="http://sharpsightlabs.com/blog/mapping-seattle-crime/">more advanced visualizations</a> and analyses.</p>
<h1>Sign up to master R</h1>
<p>Want to master R?</p>
<p>Here at Sharp Sight, we teach data science in R.</p>
<p>And we not only show you the techniques, we will show you how to practice those techniques so that you master them.</p>
<p>So if you want to master data science and become “fluent” in R, sign up for our email newsletter.</p>
<p>When you sign up, you’ll get:<br>
– ggplot2 data visualization tutorials<br>
– tutorials on how to practice ggplot2 syntax (so you can write code “with your eyes closed”)<br>
– practice tips to help you master and memorize syntax</p>
<p>You’ll also get access to our “Data Science Crash Course” for free.</p>
<p><a rel="nofollow" href="http://sharpsightlabs.com/join-newsletter-ggplot2/" target="_blank">
<p><strong>SIGN UP NOW</strong></p>
<p></p></a>
<p>The post <a rel="nofollow" href="http://sharpsightlabs.com/blog/basic-maps-ggmap/" target="_blank">How to plot basic maps with ggmap</a> appeared first on <a rel="nofollow" href="http://sharpsightlabs.com/" target="_blank">SHARP SIGHT LABS</a>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://sharpsightlabs.com/blog/basic-maps-ggmap/"> r-bloggers – SHARP SIGHT LABS</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/quWnwlxs79E" height="1"></a></div>