<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/using-leaflet-just-because/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://nsaunders.wordpress.com/2018/07/17/using-leaflet-just-because/"> R – What You're Doing Is Rather Desperate</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I love it when researchers take the time to share their knowledge of the computational tools that they use. So first, let me point you at <a rel="nofollow" href="http://environmentalcomputing.net/about-this-site/" target="_blank">Environmental Computing</a>, a site run by environmental scientists at the University of New South Wales, which has a good selection of R programming tutorials.</p>
<p>One of these is <a rel="nofollow" href="http://environmentalcomputing.net/making-maps-of-your-study-sites/" target="_blank">Making maps of your study sites</a>. It was written with the specific purpose of generating simple, clean figures for publications and presentations, which it achieves very nicely.</p>
<p>I’ll be honest: the sole motivator for this post is that I thought it would be fun to generate the map using <a rel="nofollow" href="https://rstudio.github.io/leaflet/" target="_blank">Leaflet for R</a> as an alternative. You might use Leaflet if you want:</p>
<ul>
<li>An interactive map that you can drag, zoom, click for popup information</li>
<li>A “fancier” static map with geographical features of interest</li>
<li>concise and clean code which uses pipes and doesn’t require that you process shapefiles</li>
</ul>
<p><span></span></p>
<p>Without further ado:</p>
<pre>
library(leaflet)
library(readr)
point_data &lt;- read_csv(&quot;http://environmentalcomputing.net/wp-content/uploads/2018/03/point_data.csv&quot;)

leaflet(point_data) %>% 
  fitBounds(lng1 = min(point_data$lon) - 0.11, 
            lat1 = min(point_data$lat) - 0.11,
            lng2 = max(point_data$lon) + 0.11, 
            lat2 = max(point_data$lat) + 0.11) %>% 
  addCircleMarkers(~lon, ~lat,
                   radius = 3,
                   opacity = 100,
                   color = &quot;white&quot;, 
                   label = as.character(point_data$Site), 
                   labelOptions = labelOptions(noHide = TRUE, 
                                               textOnly = TRUE, 
                                               style = list(color = &quot;white&quot;),
                                               direction = &quot;auto&quot;,
                                               offset = c(0, -10))) %>% 
  addScaleBar(options = list(imperial = FALSE)) %>%
  addProviderTiles(providers$Esri.WorldImagery)
</pre>
<p><div data-shortcode="caption" class="wp-caption"><a rel="nofollow" href="https://nsaunders.files.wordpress.com/2018/07/esri_world_imagery.png" target="_blank"><img data-orig-file="https://nsaunders.files.wordpress.com/2018/07/esri_world_imagery.png" data-orig-size="1742,1009" data-medium-file="https://nsaunders.files.wordpress.com/2018/07/esri_world_imagery.png?w=300" data-attachment-id="5085" data-image-meta="{" data-image-description="" src="https://nsaunders.files.wordpress.com/2018/07/esri_world_imagery.png?w=450" data-permalink="https://nsaunders.wordpress.com/2018/07/17/using-leaflet-just-because/esri_world_imagery/" data-large-file="https://nsaunders.files.wordpress.com/2018/07/esri_world_imagery.png?w=1024" alt="" data-image-title="esri_world_imagery" sizes="(max-width: 500px) 100vw, 500px" data-comments-opened="1" class="wp-image-5085" data-recalc-dims="1"></a>
<p class="wp-caption-text">Esri.WorldImagery</p>
</div>
<p>Building a Leaflet map is similar to <code>ggplot2</code>, except that the pipe symbol is used to add layers instead of “+”. In the example above we create a leaflet object from the coordinate data file, add a bounding box based on the latitude/longitude values, add markers and a scale bar.<br>
In the final step we add a map tile from a provider. The example here is named <code>Esri.WorldImagery</code>. You can preview the complete set of providers <a rel="nofollow" href="http://leaflet-extras.github.io/leaflet-providers/preview/index.html" target="_blank">at this site</a>.</p>
<p><div data-shortcode="caption" class="wp-caption"><a rel="nofollow" href="https://nsaunders.files.wordpress.com/2018/07/hydda_base.png" target="_blank"><img data-orig-file="https://nsaunders.files.wordpress.com/2018/07/hydda_base.png" data-orig-size="1744,1014" data-medium-file="https://nsaunders.files.wordpress.com/2018/07/hydda_base.png?w=300" data-attachment-id="5091" data-image-meta="{" data-image-description="" src="https://nsaunders.files.wordpress.com/2018/07/hydda_base.png?w=450" data-permalink="https://nsaunders.wordpress.com/2018/07/17/using-leaflet-just-because/hydda_base/" data-large-file="https://nsaunders.files.wordpress.com/2018/07/hydda_base.png?w=1024" alt="" data-image-title="hydda_base" sizes="(max-width: 500px) 100vw, 500px" data-comments-opened="1" class="wp-image-5091" data-recalc-dims="1"></a>
<p class="wp-caption-text">Hydda.Base</p>
</div>
<p>Leaflet maps are designed to be pretty, so there are not many provider tiles that might be called plain or minimalist. For something less “satellite imagery” and more “traditional map”, try changing the provider to <code>Hydda.Base</code>.</p>
<p><div data-shortcode="caption" class="wp-caption"><a rel="nofollow" href="https://nsaunders.files.wordpress.com/2018/07/esri_world_gray_canvas.png" target="_blank"><img data-orig-file="https://nsaunders.files.wordpress.com/2018/07/esri_world_gray_canvas.png" data-orig-size="1743,1015" data-medium-file="https://nsaunders.files.wordpress.com/2018/07/esri_world_gray_canvas.png?w=300" data-attachment-id="5093" data-image-meta="{" data-image-description="" src="https://nsaunders.files.wordpress.com/2018/07/esri_world_gray_canvas.png?w=450" data-permalink="https://nsaunders.wordpress.com/2018/07/17/using-leaflet-just-because/esri_world_gray_canvas/" data-large-file="https://nsaunders.files.wordpress.com/2018/07/esri_world_gray_canvas.png?w=1024" alt="" data-image-title="esri_world_gray_canvas" sizes="(max-width: 500px) 100vw, 500px" data-comments-opened="1" class="wp-image-5093" data-recalc-dims="1"></a>
<p class="wp-caption-text">Esri.WorldGrayCanvas</p>
</div>
<p>In the final example, we change the label colour to black and use the Esri.WorldGrayCanvas, which is about as plain and monochrome as Leaflet gets.</p>
<p>If you use RStudio, Leaflet maps are rendered in the Viewer pane, from where you can export to HTML or image files (though the latter is a little broken for me right now). RStudio also makes it easy to explore and install other useful Leaflet-associated packages, such as <a rel="nofollow" href="https://github.com/bhaskarvk/leaflet.extras" target="_blank">leaflet.extras</a>.</p>
<p>So that’s Leaflet for R. One issue is that there are quite a few new functions to learn, some of which have many options. Fortunately the <a rel="nofollow" href="https://rstudio.github.io/leaflet/" target="_blank">introductory documentation</a> is very good.</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://nsaunders.wordpress.com/2018/07/17/using-leaflet-just-because/"> R – What You're Doing Is Rather Desperate</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/wIp2hxoVe50" height="1"></a></div>