<p>So the goal here is to focus back on Greenville County and have even more granularity. I look at median house prices near Greenville and then overlay the park data downloaded earlier. This time, for the Census data, I use the <code>tidycensus</code> package that came out recently. Furthermore, instead of using <code>ggplot2</code> to create a static map, I use the <code>leaflet</code> package to create an interactive map, and, furthermore integrate data from disparate sources in a convenient way.</p>
<h1>Download the local park data</h1>
<p>The local parks file can be found <a rel="nofollow" href="https://data.openupstate.org/map-layers" target="_blank">here</a> courtesy of a small group of dedicated volunteers and an API that makes publishing geojson files easy at the Open Upstate site. We will download a polygon file for the park boundaries as well as a point geojson file for the address of each park.</p>
<figure>
<pre><code data-lang="r"><span>data_url</span><span> </span><span>&lt;-</span><span> </span><span>&quot;https://data.openupstate.org/maps/city-parks/parks.php&quot;</span><span>
</span><span>data_file</span><span> </span><span>&lt;-</span><span> </span><span>&quot;parks.geojson&quot;</span><span>
</span><span># for some reason, I can't read from the url directly, though the tutorial
# says I can
</span><span>download.file</span><span>(</span><span>data_url</span><span>,</span><span> </span><span>data_file</span><span>)</span><span>
</span><span>data_park</span><span> </span><span>&lt;-</span><span> </span><span>geojson_read</span><span>(</span><span>data_file</span><span>,</span><span> </span><span>what</span><span> </span><span>=</span><span> </span><span>&quot;sp&quot;</span><span>)</span><span>
</span><span>data_url</span><span> </span><span>&lt;-</span><span> </span><span>&quot;https://data.openupstate.org/maps/city-parks/geojson.php&quot;</span><span>
</span><span>data_file</span><span> </span><span>&lt;-</span><span> </span><span>&quot;parks_point.geojson&quot;</span><span>
</span><span># for some reason, I can't read from the url directly, though the tutorial
# says I can
</span><span>download.file</span><span>(</span><span>data_url</span><span>,</span><span> </span><span>data_file</span><span>)</span><span>
</span><span>data_park_addr</span><span> </span><span>&lt;-</span><span> </span><span>geojson_read</span><span>(</span><span>data_file</span><span>,</span><span> </span><span>what</span><span> </span><span>=</span><span> </span><span>&quot;sp&quot;</span><span>)</span></code></pre>
</figure>
<h1>Download the median home value data</h1>
<p>This code from <code>tidycensus</code> downloads demographic data <em>and</em> geometric data in a list column. A list column is a data frame, but one of the variables really contains a spatial data frame for each observation, which gives the polygon data for the census tracts. Having the demographic and geometric data in this format eases bookkeeping, and, thankfully, leaflet understands this format.</p>
<figure>
<pre><code data-lang="r"><span>gvl_value</span><span> </span><span>&lt;-</span><span> </span><span>get_acs</span><span>(</span><span>geography</span><span> </span><span>=</span><span> </span><span>&quot;tract&quot;</span><span>,</span><span> </span><span>variables</span><span> </span><span>=</span><span> </span><span>&quot;B25077_001&quot;</span><span>,</span><span> </span><span>state</span><span> </span><span>=</span><span> </span><span>&quot;SC&quot;</span><span>,</span><span>
</span><span>county</span><span> </span><span>=</span><span> </span><span>&quot;Greenville County&quot;</span><span>,</span><span>
</span><span>geometry</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span></code></pre>
</figure>
<h1>Plot the census and local data together</h1>
<p>Now we bring everything together. The <code>leaflet</code> package was written to make extensive use of the pipe operator that <code>dplyr</code> introduced a few years ago. We can set a default data frame for a leaflet map, but when we add markers and polygons, we can set it from other data sources. The following code is one way to do this, where we use the <code>tidycensus</code>-generated dataset as the foundation of the leaflet. We add polygons and markers for the data park using the <code>data=</code> option of <code>addPolygons</code> and <code>addMarkers</code>. Note the use of the <code>group=</code> option to create layers, which can be clicked on and off interactively. The <code>label=</code> option (or <code>popup=</code> option for addPolygons) are used to generate popup windows that give additional information.</p>
<figure>
<pre><code data-lang="r"><span>pal</span><span> </span><span>&lt;-</span><span> </span><span>colorNumeric</span><span>(</span><span>palette</span><span> </span><span>=</span><span> </span><span>&quot;viridis&quot;</span><span>,</span><span> </span><span>domain</span><span> </span><span>=</span><span> </span><span>gvl_value</span><span>$</span><span>estimate</span><span>)</span><span>
</span><span>gvl_value</span><span> </span><span>%>%</span><span>
</span><span>st_transform</span><span>(</span><span>crs</span><span> </span><span>=</span><span> </span><span>&quot;+init=epsg:4326&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>leaflet</span><span>(</span><span>width</span><span> </span><span>=</span><span> </span><span>&quot;100%&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>addProviderTiles</span><span>(</span><span>provider</span><span> </span><span>=</span><span> </span><span>&quot;CartoDB.Positron&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>addPolygons</span><span>(</span><span>popup</span><span> </span><span>=</span><span> </span><span>~</span><span> </span><span>str_extract</span><span>(</span><span>NAME</span><span>,</span><span> </span><span>&quot;^([^,]*)&quot;</span><span>),</span><span>
</span><span>stroke</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span>
</span><span>smoothFactor</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span>
</span><span>fillOpacity</span><span> </span><span>=</span><span> </span><span>0.5</span><span>,</span><span>
</span><span>color</span><span> </span><span>=</span><span> </span><span>~</span><span> </span><span>pal</span><span>(</span><span>estimate</span><span>),</span><span>
</span><span>group</span><span>=</span><span>&quot;Median home value&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>addLegend</span><span>(</span><span>&quot;bottomright&quot;</span><span>,</span><span> </span><span>pal</span><span> </span><span>=</span><span> </span><span>pal</span><span>,</span><span> </span><span>values</span><span> </span><span>=</span><span> </span><span>~</span><span> </span><span>estimate</span><span>,</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Median home value&quot;</span><span>,</span><span>
</span><span>labFormat</span><span> </span><span>=</span><span> </span><span>labelFormat</span><span>(</span><span>prefix</span><span> </span><span>=</span><span> </span><span>&quot;$&quot;</span><span>),</span><span>
</span><span>opacity</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>%>%</span><span> </span><span>addPolygons</span><span>(</span><span>data</span><span>=</span><span>data_park</span><span>,</span><span>fillOpacity</span><span>=</span><span>0.8</span><span>,</span><span>group</span><span>=</span><span>&quot;Parks&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span>addMarkers</span><span>(</span><span>data</span><span>=</span><span>data_park_addr</span><span>,</span><span>group</span><span>=</span><span>&quot;Parks&quot;</span><span>,</span><span>label</span><span>=~</span><span>title</span><span>)</span><span> </span><span>%>%</span><span> </span><span>addLayersControl</span><span>(</span><span>overlayGroups</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;Parks&quot;</span><span>,</span><span>&quot;Median home value&quot;</span><span>))</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-4" src="https://i2.wp.com/randomjohn.github.io/figures//2017-07-21-r-maps-with-leaflet.Rmdunnamed-chunk-4-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Unfortunately due to the limitations of Github pages this had to be turned into a static image to be rendered. Perhaps it’s time to make the jump to blogdown and hugo like all the other cool kids?</p>
<h1>Discussion</h1>
<p>I’m just starting to learn about the <code>leaflet</code> package, but in just a couple of hours (and standing on the shoulders of giants) I was able to put together an interactive map combining Census data (median home value by census tract) and locally-generated data (park locations). Such combinations can be effectively used to examine local situations in the context of rich data already collected at a federal level (assuming the instability at the U.S. Census Bureau is temporary).</p>
	<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/Qfv5PJsy8nc" height="1">