<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/mapping-data-using-r-and-leaflet/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://nsaunders.wordpress.com/2017/11/15/mapping-data-using-r-and-leaflet/"> 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>The R language provides many different tools for creating maps and adding data to them. I’ve been using the <a rel="nofollow" href="https://rstudio.github.io/leaflet/" target="_blank">leaflet package</a> at work recently, so I thought I’d provide a short example here.</p>
<p>Whilst searching for some data that might make a nice map, I came across <a rel="nofollow" href="http://www.abc.net.au/news/2017-11-14/same-sex-marriage-if-the-survey-says-yes-how-will-your-mp-vote/9104112" target="_blank">this article at ABC News</a>. It includes a table containing Australian members of parliament, their electorate and their voting intention regarding legalisation of same-sex marriage. Since I reside in New South Wales, let’s map the data for electorates in that state. </p>
<p><span></span></p>
<p><a rel="nofollow" href="https://github.com/neilfws/politics/blob/master/AusVoteSSM/code/R/ssm_voting_mps.Rmd" target="_blank">Here’s the code at Github</a>. The procedure is pretty straightforward:</p>
<ul>
<li>Obtain a shapefile of New South Wales electorates (<a rel="nofollow" href="http://www.aec.gov.au/Electorates/gis/gis_datadownload.htm" target="_blank">from here</a>) and read into R</li>
<li>Read the data from the ABC News web page into a data frame (very easy using <code>rvest</code>)</li>
<li>Match the electorate names from the two sources (they match perfectly, well done ABC!)</li>
<li>Use the match to add a voting intention variable to the shapefile data</li>
<li>Use leaflet to generate the map of New South Wales with electorates coloured by voting intention</li>
</ul>
<p><a rel="nofollow" href="https://nsaunders.wordpress.com/2017/11/15/mapping-data-using-r-and-leaflet/ssm_rpubs/" target="_blank"><img data-orig-file="https://nsaunders.files.wordpress.com/2017/11/ssm_rpubs.png" data-orig-size="1668,868" data-medium-file="https://nsaunders.files.wordpress.com/2017/11/ssm_rpubs.png?w=300&amp;h=156&amp;fit=300%2C156&amp;resize=300%2C156" data-attachment-id="4974" data-image-meta="{" data-image-description="" src="https://nsaunders.files.wordpress.com/2017/11/ssm_rpubs.png?w=300&h=156&fit=300%2C156&resize=300%2C156" data-permalink="https://nsaunders.wordpress.com/2017/11/15/mapping-data-using-r-and-leaflet/ssm_rpubs/" data-large-file="https://nsaunders.files.wordpress.com/2017/11/ssm_rpubs.png?w=1024" alt="" data-image-title="ssm_rpubs" sizes="(max-width: 300px) 100vw, 300px" data-comments-opened="1" class="wp-image-4974" data-recalc-dims="1"></a>At right, a non-interactive screen grab of the result (click for larger version). For the full interactive map visit <a rel="nofollow" href="http://rpubs.com/neilfws/329940" target="_blank">this RPubs page</a> where you can zoom, drag and mouse-over to see how Leaflet maps work.</p>
<p>I like R/leaflet a lot. It generates high-quality interactive maps and it’s easy to experiment in and publish from Rstudio. Some of the syntax feels a little clunky (a mix of newer “pipes” and older “formula-style”) and generating colour palettes feels strange if you spend most time in ggplot2. However, some of that is probably my inexperience with the package as much as anything else.</p>
<p>Filed under: <a rel="nofollow" href="https://nsaunders.wordpress.com/category/australia/" target="_blank">australia</a>, <a rel="nofollow" href="https://nsaunders.wordpress.com/category/australian-news/" target="_blank">australian news</a>, <a rel="nofollow" href="https://nsaunders.wordpress.com/category/statistics/r/" target="_blank">R</a>, <a rel="nofollow" href="https://nsaunders.wordpress.com/category/statistics/" target="_blank">statistics</a> Tagged: <a rel="nofollow" href="https://nsaunders.wordpress.com/tag/leaflet/" target="_blank">leaflet</a>, <a rel="nofollow" href="https://nsaunders.wordpress.com/tag/maps/" target="_blank">maps</a>, <a rel="nofollow" href="https://nsaunders.wordpress.com/tag/politics/" target="_blank">politics</a>, <a rel="nofollow" href="https://nsaunders.wordpress.com/tag/ssm/" target="_blank">ssm</a> <a rel="nofollow" href="https://feeds.wordpress.com/1.0/gocomments/nsaunders.wordpress.com/4972/" target="_blank"><img border="0" alt="" src="https://feeds.wordpress.com/1.0/comments/nsaunders.wordpress.com/4972/"></a> <img border="0" alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" 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://nsaunders.wordpress.com/2017/11/15/mapping-data-using-r-and-leaflet/"> 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/aP-St9hRLLo" height="1"></a></div>