<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/map-danish-administrative-areas-with-leaflet/"></div></div><div><a data-url="http://www.r-bloggers.com/map-danish-administrative-areas-with-leaflet/" data-counturl="http://www.r-bloggers.com/map-danish-administrative-areas-with-leaflet/" data-count="horizontal" data-text="Map danish administrative areas with leaflet" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.56n.dk/map-danish-administrative-areas-with-leaflet/"> Renglish – 56north | Skræddersyet dataanalyse</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<article>
<h2><a target="_blank" href="https://github.com/56north/leafletDK#what-is-this"></a>What is this?</h2>
<p><a target="_blank" href="https://youtu.be/7ffj8SHrbk0">a center for ants!?</a></p>
<p>leafletDK is a package that makes it easy to create interactive dynamic leaflet maps based on danish administrative areas.</p>
<p>The package is heavily inspired by the amazing <a target="_blank" href="https://github.com/sebastianbarfort/mapDK">mapDK</a> package by <a target="_blank" href="https://twitter.com/sbarfort">Sebastian Barfort</a>, which I recommend if you want to create static high quality maps.</p>
<h2><a target="_blank" href="https://github.com/56north/leafletDK#getting-started"></a>Getting started</h2>
<p>First you need to install the package from github. You do this by running:</p>
<pre><code>devtools::install_github(&quot;56north/leafletDK&quot;) # install devtools if needed
</code></pre>
<p>After install it is really easy to use leafletDK. Simply call the administrative area that you want to map (like a municipality) and give it the data you want the map to be colored by.</p>
<p>Below is an example where we load data from Statistics Denmark and map it using leaflet.</p>
<p>First we load the package and get the most recent population count for Denmark via the API from Statistics Denmark.</p>
<pre><code>library(leafletDK)

folk1 &lt;- read.csv2(&quot;http://api.statbank.dk/v1/data/folk1/CSV?OMR%C3%85DE=*&quot;,
                   stringsAsFactors = F)
</code></pre>
<p>Now we have a data frame with three columns. “OMRÅDE” is the area, “TID” is time/date and “INDHOLD” is the people count. We will use the “OMRÅDE” and “INDHOLD” columns to call the municipalityDK function.</p>
<pre><code>municipalityDK(&quot;INDHOLD&quot;, &quot;OMRÅDE&quot;, data = folk1)
</code></pre>
<p><a target="_blank" href="https://github.com/56north/leafletDK/blob/master/nonpackage/readme_plot1.png"><img alt="" src="https://github.com/56north/leafletDK/raw/master/nonpackage/readme_plot1.png"></a></p>
<p>As default leafletDK plots the map without an underlying map. You can turn this on by supplying the parameter <code>map = T</code>. You can also turn on the legend with <code>legend = T</code>.</p>
<pre><code>municipalityDK(&quot;INDHOLD&quot;, &quot;OMRÅDE&quot;, data = folk1, map = T, legend = T)
</code></pre>
<p><a target="_blank" href="https://github.com/56north/leafletDK/blob/master/nonpackage/readme_plot2.png"><img alt="" src="https://github.com/56north/leafletDK/raw/master/nonpackage/readme_plot2.png"></a></p>
<p>This generates a map of Denmark where the 98 municipalities are colores according to the amount of people that live in each on. It becomes immediately apparent that a lot of people are living in Copenhagen municipality… a lot of people!</p>
<p>We can also zoom in the map on only a few select municipalities by selecting them with the subplot parameter. Lets take a look at Copenhagen (København), Frederiksberg and Hvidovre municipalities:</p>
<pre><code>municipalityDK(&quot;INDHOLD&quot;, &quot;OMRÅDE&quot;, 
                subplot = c(&quot;københavn&quot;, &quot;frederiksberg&quot;, &quot;hvidovre&quot;), 
                data = folk1)
</code></pre>
<p><a target="_blank" href="https://github.com/56north/leafletDK/blob/master/nonpackage/readme_plot3.png"><img alt="" src="https://github.com/56north/leafletDK/raw/master/nonpackage/readme_plot3.png"></a></p>
<p>This generates a map with only our three chosen municipalities. If you click on one of the areas, a little popup appears with the mapped values.</p>
<h2><a target="_blank" href="https://github.com/56north/leafletDK#getting-ids"></a>Getting ids</h2>
<p>If you are in doubt of what ids are being used to generate the maps, then you can use the <code>getIDs</code> function to see a list. If we want to get a list of the municipalities we will do the following:</p>
<pre><code>getIDs(&quot;municipal&quot;)
</code></pre>
<p>The getIDs function accepts the following areas: “constituency”, “district”, “municipal”, “parish”, “regional”, “rural” or “zip”.</p>
<h2><a target="_blank" href="https://github.com/56north/leafletDK#changing-the-underlying-map"></a>Changing the underlying map</h2>
<p>You can change the underlying map by using the <code>addProviderTiles()</code> from the leaflet package. You can pipe the mapped areas directly to the function like this:</p>
<pre><code>municipalityDK(&quot;INDHOLD&quot;, &quot;OMRÅDE&quot;, 
                subplot = c(&quot;københavn&quot;, &quot;frederiksberg&quot;, &quot;hvidovre&quot;), 
                data = folk1) %>% 
                addProviderTiles(&quot;Stamen.Toner&quot;)
</code></pre>
<p><a target="_blank" href="https://github.com/56north/leafletDK/blob/master/nonpackage/readme_plot4.png"><img alt="" src="https://github.com/56north/leafletDK/raw/master/nonpackage/readme_plot4.png"></a></p>
<p>You can get a full overview of the available maps (called tiles) on the <a target="_blank" href="http://leaflet-extras.github.io/leaflet-providers/preview/">leaflet-providers preview page</a></p>
</article>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/map-danish-administrative-areas-with-leaflet/"></div></div><div><a data-url="http://www.r-bloggers.com/map-danish-administrative-areas-with-leaflet/" data-counturl="http://www.r-bloggers.com/map-danish-administrative-areas-with-leaflet/" data-count="vertical" data-text="Map danish administrative areas with leaflet" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.56n.dk/map-danish-administrative-areas-with-leaflet/"> Renglish – 56north | Skræddersyet dataanalyse</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/exVIEK-fpCg"></a></div>