<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/getting-started-stamen-maps-with-ggmap/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://statisticaloddsandends.wordpress.com/2018/10/25/getting-started-stamen-maps-with-ggmap/"> R – Statistical Odds &amp; Ends</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Spatial visualizations really come to life when you have a real map as a background. In R, <code>ggmap</code> is the package that you’ll want to use to get these maps. In what follows, we’ll demonstrate how to use <code>ggmap</code> with the <code>Sacramento</code> dataset in the <code>caret</code> package. For each city, we are going to keep track of the median price of houses in that city, the number of transactions in that city, as well as the mean latitude and longitude of the transactions.</p>
<pre>
library(caret)
library(dplyr)
library(ggmap)

data(Sacramento)
df &lt;- Sacramento %>% group_by(city) %>%
    summarize(median_price = median(price), transactions = n(),
              latitude = mean(latitude), longitude = mean(longitude))
</pre>
<p>Now, I want to plot these cities on a map, with the color of the points representing the median price and the size representing the number of transactions. I could do this with <code>ggplot2</code>:</p>
<pre>
ggplot() + 
    geom_point(data = df, mapping = aes(x = longitude, y = latitude, 
            col = median_price, size = transactions)) +
    scale_color_distiller(palette = &quot;YlOrRd&quot;, direction = 1)
</pre>
<p><img data-orig-file="https://statisticaloddsandends.files.wordpress.com/2018/10/ggplot.png?w=450&amp;h=389&amp;fit=584%2C389" data-orig-size="600,400" data-medium-file="https://statisticaloddsandends.files.wordpress.com/2018/10/ggplot.png?w=450&amp;h=389&amp;fit=584%2C389?w=300" data-attachment-id="502" data-image-meta="{" data-image-description="" src="https://statisticaloddsandends.files.wordpress.com/2018/10/ggplot.png?w=450&h=389&fit=584%2C389" data-permalink="https://statisticaloddsandends.wordpress.com/2018/10/25/getting-started-stamen-maps-with-ggmap/ggplot/" data-large-file="https://statisticaloddsandends.files.wordpress.com/2018/10/ggplot.png?w=450&amp;h=389&amp;fit=584%2C389?w=584" alt="" data-image-title="ggplot" sizes="(max-width: 584px) 100vw, 584px" data-comments-opened="1" class="wp-image-502" data-recalc-dims="1"></p>
<p>That plots the data that we want, but the visualization isn’t very informative. For it to be useful, I need to know which point represents which city. I could add text labels to each of these points…</p>
<pre>
ggplot(data = df, mapping = aes(x = longitude, y = latitude)) + 
    geom_point(aes(col = median_price, size = transactions)) +
    geom_text(aes(label = city), size = 2, nudge_y = 0.01) +
    scale_color_distiller(palette = &quot;YlOrRd&quot;, direction = 1)
</pre>
<p><img data-orig-file="https://statisticaloddsandends.files.wordpress.com/2018/10/ggplot2.png?w=450&amp;h=389&amp;fit=584%2C389" data-orig-size="600,400" data-medium-file="https://statisticaloddsandends.files.wordpress.com/2018/10/ggplot2.png?w=450&amp;h=389&amp;fit=584%2C389?w=300" data-attachment-id="504" data-image-meta="{" data-image-description="" src="https://statisticaloddsandends.files.wordpress.com/2018/10/ggplot2.png?w=450&h=389&fit=584%2C389" data-permalink="https://statisticaloddsandends.wordpress.com/2018/10/25/getting-started-stamen-maps-with-ggmap/ggplot2/" data-large-file="https://statisticaloddsandends.files.wordpress.com/2018/10/ggplot2.png?w=450&amp;h=389&amp;fit=584%2C389?w=584" alt="" data-image-title="ggplot2" sizes="(max-width: 584px) 100vw, 584px" data-comments-opened="1" class="wp-image-504" data-recalc-dims="1"></p>
<p>… but it would clutter the visualization, and I still think that it does a poorer job of being informative than putting a map as background. Let’s use <code>ggmap</code> to do that!</p>
<p><strong>Google maps</strong></p>
<p>A quick note about Google maps: it seems that using Google maps as a background is quite a challenge now. According to the author of the <code>ggmap</code> package, to do so you need to provide an API key and enable billing. (See <a rel="nofollow" href="https://github.com/dkahle/ggmap" target="_blank">https://github.com/dkahle/ggmap</a> for more details on how to work with these new restrictions.)</p>
<p><strong>Stamen maps</strong></p>
<p>Fortunately there is an easier way to get map backgrounds: Stamen maps. In the code below, I’m getting a base map of the Sacramento region based on the latitudes and longitudes I see in my dataset:</p>
<pre>
height &lt;- max(Sacramento$latitude) - min(Sacramento$latitude)
width &lt;- max(Sacramento$longitude) - min(Sacramento$longitude)
sac_borders &lt;- c(bottom  = min(Sacramento$latitude)  - 0.1 * height, 
                 top     = max(Sacramento$latitude)  + 0.1 * height,
                 left    = min(Sacramento$longitude) - 0.1 * width,
                 right   = max(Sacramento$longitude) + 0.1 * width)

map &lt;- get_stamenmap(sac_borders, zoom = 10, maptype = &quot;toner-lite&quot;)
</pre>
<p>I can then print it with the <code>ggmap</code> function:</p>
<pre>
ggmap(map)
</pre>
<p><img data-orig-file="https://statisticaloddsandends.files.wordpress.com/2018/10/stamen1.png?w=450&amp;h=417&amp;fit=584%2C417" data-orig-size="1344,960" data-medium-file="https://statisticaloddsandends.files.wordpress.com/2018/10/stamen1.png?w=450&amp;h=417&amp;fit=584%2C417?w=300" data-attachment-id="508" data-image-meta="{" data-image-description="" src="https://statisticaloddsandends.files.wordpress.com/2018/10/stamen1.png?w=450&h=417&fit=584%2C417" data-permalink="https://statisticaloddsandends.wordpress.com/2018/10/25/getting-started-stamen-maps-with-ggmap/stamen1/" data-large-file="https://statisticaloddsandends.files.wordpress.com/2018/10/stamen1.png?w=450&amp;h=417&amp;fit=584%2C417?w=584" alt="" data-image-title="stamen1" sizes="(max-width: 584px) 100vw, 584px" data-comments-opened="1" class="wp-image-508" data-recalc-dims="1"></p>
<p>The <code>zoom</code> parameter is set to 10 by default. Making it bigger gives a more detailed map, but takes longer to load.</p>
<p>Just like in <code>ggplot2</code> graphics, we can add layers to the map drawn by <code>ggmap(map)</code> with the + syntax. For example, if we wanted to overlay the map with the data from our dataset, we could do the following:</p>
<pre>
ggmap(map) +
    geom_point(data = df, mapping = aes(x = longitude, y = latitude, 
                                        col = median_price, size = transactions)) +
    scale_color_distiller(palette = &quot;YlOrRd&quot;, direction = 1)
</pre>
<p><img data-orig-file="https://statisticaloddsandends.files.wordpress.com/2018/10/stamen2.png?w=450&amp;h=417&amp;fit=584%2C417" data-orig-size="1344,960" data-medium-file="https://statisticaloddsandends.files.wordpress.com/2018/10/stamen2.png?w=450&amp;h=417&amp;fit=584%2C417?w=300" data-attachment-id="510" data-image-meta="{" data-image-description="" src="https://statisticaloddsandends.files.wordpress.com/2018/10/stamen2.png?w=450&h=417&fit=584%2C417" data-permalink="https://statisticaloddsandends.wordpress.com/2018/10/25/getting-started-stamen-maps-with-ggmap/stamen2/" data-large-file="https://statisticaloddsandends.files.wordpress.com/2018/10/stamen2.png?w=450&amp;h=417&amp;fit=584%2C417?w=584" alt="" data-image-title="stamen2" sizes="(max-width: 584px) 100vw, 584px" data-comments-opened="1" class="wp-image-510" data-recalc-dims="1"></p>
<p>Another way to get Stamen maps as a background is to use the <code>qmplot</code> function. Notice though that the syntax is pretty different from what we had with <code>ggplot</code>. Here is a map plotting the same points as above but with a different color scale and a different type of background map:</p>
<pre>
qmplot(x = longitude, y = latitude, data = df, maptype = &quot;watercolor&quot;, 
       geom = &quot;point&quot;, color = median_price, size = transactions) +
    scale_color_gradient(low = &quot;blue&quot;, high = &quot;red&quot;)
</pre>
<p><img data-orig-file="https://statisticaloddsandends.files.wordpress.com/2018/10/stamen3.png?w=450&amp;h=417&amp;fit=584%2C417" data-orig-size="1344,960" data-medium-file="https://statisticaloddsandends.files.wordpress.com/2018/10/stamen3.png?w=450&amp;h=417&amp;fit=584%2C417?w=300" data-attachment-id="512" data-image-meta="{" data-image-description="" src="https://statisticaloddsandends.files.wordpress.com/2018/10/stamen3.png?w=450&h=417&fit=584%2C417" data-permalink="https://statisticaloddsandends.wordpress.com/2018/10/25/getting-started-stamen-maps-with-ggmap/stamen3/" data-large-file="https://statisticaloddsandends.files.wordpress.com/2018/10/stamen3.png?w=450&amp;h=417&amp;fit=584%2C417?w=584" alt="" data-image-title="stamen3" sizes="(max-width: 584px) 100vw, 584px" data-comments-opened="1" class="wp-image-512" data-recalc-dims="1"></p>
<p>The full list of Stamen map types is “terrain”, “terrain-background”, “terrain-labels”, “terrain-lines”, “toner”, “toner-2010”, “toner-2011”, “toner-background”, “toner-hybrid”, “toner-labels”, “toner-lines”, “toner-lite”, “watercolor”. Note that some of them don’t work with the CRAN version of <code>ggmap</code> (e.g. “terrain”); instead, you will have to download the github version of the package (see <a rel="nofollow" href="https://github.com/dkahle/ggmap/issues/188" target="_blank">https://github.com/dkahle/ggmap/issues/188</a> for more details).</p>
<p>Below are examples of what “terrain” and “toner” look like:</p>
<pre>
qmplot(x = longitude, y = latitude, data = df, maptype = &quot;terrain&quot;, 
       geom = &quot;point&quot;, color = median_price, size = transactions) +
    scale_color_gradient(low = &quot;blue&quot;, high = &quot;red&quot;)
</pre>
<p><img data-orig-file="https://statisticaloddsandends.files.wordpress.com/2018/10/stamen4.png?w=450&amp;h=417&amp;fit=584%2C417" data-orig-size="1344,960" data-medium-file="https://statisticaloddsandends.files.wordpress.com/2018/10/stamen4.png?w=450&amp;h=417&amp;fit=584%2C417?w=300" data-attachment-id="514" data-image-meta="{" data-image-description="" src="https://statisticaloddsandends.files.wordpress.com/2018/10/stamen4.png?w=450&h=417&fit=584%2C417" data-permalink="https://statisticaloddsandends.wordpress.com/2018/10/25/getting-started-stamen-maps-with-ggmap/stamen4/" data-large-file="https://statisticaloddsandends.files.wordpress.com/2018/10/stamen4.png?w=450&amp;h=417&amp;fit=584%2C417?w=584" alt="" data-image-title="stamen4" sizes="(max-width: 584px) 100vw, 584px" data-comments-opened="1" class="wp-image-514" data-recalc-dims="1"></p>
<pre>
qmplot(x = longitude, y = latitude, data = df, maptype = &quot;toner&quot;, 
       geom = &quot;point&quot;, color = median_price, size = transactions) +
    scale_color_gradient(low = &quot;blue&quot;, high = &quot;red&quot;)
</pre>
<p><img data-orig-file="https://statisticaloddsandends.files.wordpress.com/2018/10/stamen5.png?w=450&amp;h=417&amp;fit=584%2C417" data-orig-size="1344,960" data-medium-file="https://statisticaloddsandends.files.wordpress.com/2018/10/stamen5.png?w=450&amp;h=417&amp;fit=584%2C417?w=300" data-attachment-id="515" data-image-meta="{" data-image-description="" src="https://statisticaloddsandends.files.wordpress.com/2018/10/stamen5.png?w=450&h=417&fit=584%2C417" data-permalink="https://statisticaloddsandends.wordpress.com/2018/10/25/getting-started-stamen-maps-with-ggmap/stamen5/" data-large-file="https://statisticaloddsandends.files.wordpress.com/2018/10/stamen5.png?w=450&amp;h=417&amp;fit=584%2C417?w=584" alt="" data-image-title="stamen5" sizes="(max-width: 584px) 100vw, 584px" data-comments-opened="1" class="wp-image-515" data-recalc-dims="1"></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="https://statisticaloddsandends.wordpress.com/2018/10/25/getting-started-stamen-maps-with-ggmap/"> R – Statistical Odds &amp; Ends</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 alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/55KUCR4cSCY" height="1"></a></div>