<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Eamon O&#39;Connor on Medium]]></title>
        <description><![CDATA[Stories by Eamon O&#39;Connor on Medium]]></description>
        <link>https://medium.com/@eamon-james-oconnor?source=rss-a712101ba3ec------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*yrntLtU1q8WQHJgYKv5GPA.png</url>
            <title>Stories by Eamon O&amp;#39;Connor on Medium</title>
            <link>https://medium.com/@eamon-james-oconnor?source=rss-a712101ba3ec------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 01:09:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@eamon-james-oconnor/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Visualizing Computer Networks]]></title>
            <link>https://medium.com/swlh/visualizing-computer-networks-1d7805c00f17?source=rss-a712101ba3ec------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d7805c00f17</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[dash]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[data-visualization]]></category>
            <dc:creator><![CDATA[Eamon O'Connor]]></dc:creator>
            <pubDate>Mon, 26 Oct 2020 06:33:52 GMT</pubDate>
            <atom:updated>2020-10-27T06:19:49.461Z</atom:updated>
            <content:encoded><![CDATA[<h4>Using Dash Cytoscape to create interactive visuals based on real computer network data</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*KuHgB2bxvd5sG3kQ" /><figcaption>Photo by <a href="https://unsplash.com/@alinnnaaaa?utm_source=medium&amp;utm_medium=referral">Alina Grubnyak</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Background</h3><p>I work in Cyber Security. And if I’m being really honest, it’s not very interesting. There are plenty of tools, open-source or proprietary, that can automate many of the tedious tasks within a security role, but I always find myself more fascinated by how the tool works than by how much it’s helping me.</p><p>Cyber Security, Incident Response, Digital Forensics…They’re all very destructive by nature. But I’m a creative. How can a creative add value to an intrinsically destructive field? And how can I make my job more interesting by mixing it with my interests?</p><p>By creating tools.</p><p>This post is a walkthrough of a personal project I completed, which is a small step towards the creation of my own tool.</p><h3>The Project</h3><p>Turning thousands of lines of pcap data into interactive network visualisations.</p><p>Complete code available on my github repository.</p><p><a href="https://github.com/ejooco/computer_network_visualisation">ejooco/computer_network_visualisation</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kNZf--vLaoxOtKBOfM0v_Q.gif" /><figcaption>Image by Author</figcaption></figure><p>Too often an Incident Responder asks for a map of the network, only to discover it’s more than 2 years old and not much more than a ghost of the current network topology.</p><p>So we try to create the ‘ground truth’ of what the network actually looks like. Various tools can be used for this, netflow, nmap, tracert. This example will focus on network packet captures, but a bit of Python applied in the right doses could adapt this project to any network mapping command line tool output.</p><p>Wireshark is a great tool, but as the size of the packet capture increases, the performance drops as it tries to parse the potential hundreds of thousands of lines of traffic data.</p><p>So I began by exporting my pcap data to a csv. Which python loves.</p><h3>Parsing Script</h3><p>The script is on the repo. It’s not fancy. It was a quick and dirty creation to produce a quick result. Python is good for that. Which I love.</p><p>I managed to turn about 10,000 lines of this (data is publicly available and not licensed)</p><pre>&quot;No.&quot;,&quot;Time&quot;,&quot;Source&quot;,<em>&quot;Destination&quot;,</em>&quot;Protocol&quot;,<em>&quot;Length&quot;,</em>&quot;Info&quot;</pre><pre>&quot;1&quot;,&quot;0.000000&quot;,&quot;220.175.8.56&quot;,<em>&quot;80.171.48.1&quot;,</em>&quot;Messenger&quot;,<em>&quot;488&quot;,</em>&quot;NetrSendMessage request&quot;</pre><pre>&quot;2&quot;,&quot;300.930215&quot;,&quot;222.136.251.117&quot;,<em>&quot;80.171.48.1&quot;,</em>&quot;Messenger&quot;,<em>&quot;488&quot;,</em>&quot;NetrSendMessage request&quot;</pre><pre>&quot;3&quot;,&quot;300.931213&quot;,&quot;222.136.251.117&quot;,<em>&quot;80.171.48.1&quot;,</em>&quot;Messenger&quot;,<em>&quot;488&quot;,</em>&quot;NetrSendMessage request&quot;</pre><pre>&quot;4&quot;,&quot;860.029588&quot;,&quot;70.85.177.186&quot;,<em>&quot;80.171.48.1&quot;,</em>&quot;Messenger&quot;,<em>&quot;521&quot;,</em>&quot;NetrSendMessage request&quot;</pre><pre>&quot;5&quot;,&quot;860.030586&quot;,&quot;70.85.177.186&quot;,<em>&quot;80.171.48.1&quot;,</em>&quot;Messenger&quot;,<em>&quot;521&quot;,</em>&quot;NetrSendMessage request&quot;</pre><pre>&quot;6&quot;,&quot;919.623101&quot;,&quot;61.239.151.129&quot;,<em>&quot;80.171.48.1&quot;,</em>&quot;Messenger&quot;,<em>&quot;843&quot;,</em>&quot;NetrSendMessage request&quot;</pre><pre>+ ~10000 more lines</pre><p>Into this (feel free to scroll past) The output isn’t too interesting…yet.</p><pre>[</pre><pre>{&quot;data&quot;: {&quot;id&quot;: &quot;10.200.1.18&quot;, &quot;label&quot;: &quot;10.200.1.18&quot;}},</pre><pre>{&quot;data&quot;: {&quot;id&quot;: &quot;10.50.1.54&quot;, &quot;label&quot;: &quot;10.50.1.54&quot;}},</pre><pre>{&quot;data&quot;: {&quot;id&quot;: &quot;10.200.1.252&quot;, &quot;label&quot;: &quot;10.200.1.252&quot;}},</pre><pre>{&quot;data&quot;: {&quot;id&quot;: &quot;10.200.1.162&quot;, &quot;label&quot;: &quot;10.200.1.162&quot;}},</pre><pre>{&quot;data&quot;: {&quot;id&quot;: &quot;54.175.48.202&quot;, &quot;label&quot;: &quot;54.175.48.202&quot;}},</pre><pre>{&quot;data&quot;: {&quot;id&quot;: &quot;10.200.1.184&quot;, &quot;label&quot;: &quot;10.200.1.184&quot;}},</pre><pre>{&quot;data&quot;: {&quot;id&quot;: &quot;10.200.1.45&quot;, &quot;label&quot;: &quot;10.200.1.45&quot;}},</pre><pre>{&quot;data&quot;: {&quot;id&quot;: &quot;10.200.1.51&quot;, &quot;label&quot;: &quot;10.200.1.51&quot;}},</pre><pre>{&quot;data&quot;: {&quot;id&quot;: &quot;10.200.1.191&quot;, &quot;label&quot;: &quot;10.200.1.191&quot;}},</pre><pre>{&quot;data&quot;: {&quot;id&quot;: &quot;10.200.1.231&quot;, &quot;label&quot;: &quot;10.200.1.231&quot;}},</pre><pre>{&quot;data&quot;: {&quot;id&quot;: &quot;10.200.1.86&quot;, &quot;label&quot;: &quot;10.200.1.86&quot;}},</pre><pre>{&quot;data&quot;: {&quot;id&quot;: &quot;10.200.1.178&quot;, &quot;label&quot;: &quot;10.200.1.178&quot;}},</pre><pre>{&quot;data&quot;: {&quot;id&quot;: &quot;8.8.8.8&quot;, &quot;label&quot;: &quot;8.8.8.8&quot;}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.200.1.18&quot;, &quot;target&quot;: &quot;10.50.1.54&quot;, &quot;weight&quot;: 10901}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.50.1.54&quot;, &quot;target&quot;: &quot;10.200.1.18&quot;, &quot;weight&quot;: 10794}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.200.1.18&quot;, &quot;target&quot;: &quot;10.200.1.252&quot;, &quot;weight&quot;: 54}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.200.1.252&quot;, &quot;target&quot;: &quot;10.200.1.18&quot;, &quot;weight&quot;: 86}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.200.1.162&quot;, &quot;target&quot;: &quot;10.200.1.18&quot;, &quot;weight&quot;: 1040}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.200.1.18&quot;, &quot;target&quot;: &quot;10.200.1.162&quot;, &quot;weight&quot;: 67}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.200.1.18&quot;, &quot;target&quot;: &quot;8.8.8.8&quot;, &quot;weight&quot;: 11}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.200.1.18&quot;, &quot;target&quot;: &quot;54.175.48.202&quot;, &quot;weight&quot;: 46}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;54.175.48.202&quot;, &quot;target&quot;: &quot;10.200.1.18&quot;, &quot;weight&quot;: 29}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.200.1.18&quot;, &quot;target&quot;: &quot;10.200.1.184&quot;, &quot;weight&quot;: 45}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.200.1.184&quot;, &quot;target&quot;: &quot;10.200.1.18&quot;, &quot;weight&quot;: 70}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.200.1.45&quot;, &quot;target&quot;: &quot;10.200.1.18&quot;, &quot;weight&quot;: 1636}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.200.1.18&quot;, &quot;target&quot;: &quot;10.200.1.45&quot;, &quot;weight&quot;: 102}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.200.1.18&quot;, &quot;target&quot;: &quot;10.200.1.51&quot;, &quot;weight&quot;: 55}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.200.1.51&quot;, &quot;target&quot;: &quot;10.200.1.18&quot;, &quot;weight&quot;: 85}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.200.1.18&quot;, &quot;target&quot;: &quot;10.200.1.191&quot;, &quot;weight&quot;: 14}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.200.1.191&quot;, &quot;target&quot;: &quot;10.200.1.18&quot;, &quot;weight&quot;: 16}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.200.1.231&quot;, &quot;target&quot;: &quot;10.200.1.18&quot;, &quot;weight&quot;: 979}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.200.1.86&quot;, &quot;target&quot;: &quot;10.200.1.18&quot;, &quot;weight&quot;: 3}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.200.1.18&quot;, &quot;target&quot;: &quot;10.200.1.86&quot;, &quot;weight&quot;: 2}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.200.1.18&quot;, &quot;target&quot;: &quot;10.200.1.178&quot;, &quot;weight&quot;: 15}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.200.1.178&quot;, &quot;target&quot;: &quot;10.200.1.18&quot;, &quot;weight&quot;: 12}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.200.1.18&quot;, &quot;target&quot;: &quot;10.200.1.231&quot;, &quot;weight&quot;: 6}},</pre><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.200.1.18&quot;, &quot;target&quot;: &quot;10.200.1.231&quot;, &quot;weight&quot;: 6}}</pre><pre>]</pre><p>So we managed to turn over 10,000 lines of csv data into about 38 lines of json. The nifty thing about the script was that it created a weight attribute.</p><p>For example.</p><pre>{&quot;data&quot;: {&quot;source&quot;: &quot;10.200.1.162&quot;, &quot;target&quot;: &quot;10.200.1.18&quot;, &quot;weight&quot;: 1040}}</pre><p>See above how the weight is 1040? That means that the source connected to the target 1040 times. We don’t need to see every connection, just how many there were.</p><h3>Dash</h3><p>Dash was built by Plotly on top of Flask to integrate python data visualisations into web apps.</p><h3>Cytoscape</h3><p>According to <a href="https://js.cytoscape.org/">https://js.cytoscape.org/</a>, cytoscape.js is</p><blockquote>[A]Graph theory (network) library for visualisation and analysis.</blockquote><h3>Dash-Cytoscape</h3><p><em>“Dash Cytoscape is a graph visualization component for creating easily customizable, high-performance, interactive, and web-based networks. It extends and renders </em><a href="http://js.cytoscape.org/"><em>Cytoscape.js</em></a><em>, and offers deep integration with Dash layouts and callbacks, enabling the creation of powerful networks in conjunction with the rich collection of Dash components, as well as established computational biology and network science libraries such as Biopython and networkX.”</em></p><p><em>— xhlulu and the Dash Team, </em><a href="https://dash.plotly.com/cytoscape">https://dash.plotly.com/cytoscape</a></p><h3>Building the app</h3><p>I can’t take too much credit for this code, as the Plotly team made it so easy.</p><p>All you really need to do is copy the quickstart guide on the <a href="https://dash.plotly.com/cytoscape">dash-cytoscape page</a>, plug in your data, select your layout and run the app.</p><p>I added a json import to load my newly created json data.</p><pre>import dash</pre><pre>import dash_cytoscape as cyto</pre><pre>import dash_html_components as html</pre><pre>import json</pre><p>Create an instance for your app</p><pre>app = dash.Dash(__name__)</pre><p>Load your data</p><pre>with open(&#39;net.json&#39;, &#39;r&#39;) as f:</pre><pre>data = json.load(f)</pre><p>Create your layout</p><pre>app.layout = html.Div([</pre><pre>cyto.Cytoscape(</pre><pre><em>id</em>=&#39;cytoscape-compound&#39;,</pre><pre><em>layout</em>={</pre><pre>&#39;name&#39;: &#39;breadthfirst&#39;,</pre><pre>&#39;roots&#39;: &#39;[id = &quot;Web&quot;]&#39;</pre><pre>},</pre><pre><em>style</em>={&#39;width&#39;: &#39;100%&#39;, &#39;height&#39;: &#39;1000px&#39;},</pre><pre><em>stylesheet</em>=[</pre><pre>{</pre><pre>&#39;selector&#39;: &#39;node&#39;,</pre><pre>&#39;style&#39;: {&#39;content&#39;: &#39;data(label)&#39;}</pre><pre>},</pre><pre>{</pre><pre>&#39;selector&#39;: &#39;.segment&#39;,</pre><pre>&#39;style&#39;: {&#39;width&#39;: 5}</pre><pre>},</pre><pre>{</pre><pre>&#39;selector&#39;: &#39;.joiner&#39;,</pre><pre>&#39;style&#39;: {&#39;line-style&#39;: &#39;dashed&#39;}</pre><pre>}</pre><pre>],</pre><pre><em>elements</em>=data</pre><pre>)</pre><pre>])</pre><p>Run the server when you run the file</p><pre>if __name__ == &#39;__main__&#39;:</pre><pre>app.run_server(<em>debug</em>=True)</pre><p>And the result!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G9FPdY9s3ZmVCLXkCdl9MA.gif" /><figcaption>Image by Author</figcaption></figure><p>You can head over to my repo for more detailed instructions on creating the code, cloning the repo and inserting your own data.</p><p><a href="https://github.com/ejooco/computer_network_visualisation">ejooco/computer_network_visualisation</a></p><p>Thank you for reading. See you in the cloud.</p><p>Eamon</p><p>Twitter @ejooco</p><p>Github <a href="https://github.com/ejooco">https://github.com/ejooco</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d7805c00f17" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/visualizing-computer-networks-1d7805c00f17">Visualizing Computer Networks</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[R and Leaflet to create interactive choropleth maps.]]></title>
            <link>https://medium.com/data-science/r-and-leaflet-to-create-interactive-choropleth-maps-8515ef83e275?source=rss-a712101ba3ec------2</link>
            <guid isPermaLink="false">https://medium.com/p/8515ef83e275</guid>
            <category><![CDATA[maps]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[r]]></category>
            <category><![CDATA[leaflet]]></category>
            <category><![CDATA[data-visualization]]></category>
            <dc:creator><![CDATA[Eamon O'Connor]]></dc:creator>
            <pubDate>Fri, 24 Jul 2020 11:02:25 GMT</pubDate>
            <atom:updated>2020-07-29T13:44:04.261Z</atom:updated>
            <content:encoded><![CDATA[<h4>Visualizing Election Data Geographically</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6dZ7MgjgUGBwCgy3" /><figcaption>Photo by <a href="https://unsplash.com/@marjan_blan?utm_source=medium&amp;utm_medium=referral">Марьян Блан | @marjanblan</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Before I start, here is the link to the full interactive version host on Github Pages.</p><p><a href="https://ejooco.github.io/MapStats.github.io/">Creating Interactive Maps with Leaflet</a></p><p>I’ll preface this with: this is not a Coronavirus post.</p><p>Despite Covid-19 being a big deal in the world, it’s actually been fascinating for data scientists, and in particular data visualisation. It’s probably inspired many into the industry.</p><p>In decades to come I imagine that universities and online courses will be using Covid-19 datasets to teach data collection, wrangling, visualisation, modelling, etc.</p><p>But before that, the real king of data was arguably politics, so here I will pay fealty to the former ruler of data visualisation and give you a step-by-step of how to build an interactive choropleth map to display election results using R Studio and the Leaflet library.</p><p>Let’s dig in!</p><h3><strong>Load the libraries for the project</strong></h3><pre><strong>library</strong>(rgeos)<br><strong>library</strong>(maptools)<br><strong>library</strong>(leaflet)</pre><h3><strong>The Data</strong></h3><p>We’ll need 2 different sets of data. One with the mapping geometry, and one with the election results.</p><p>The data I’m using is from the Australian Electoral Commission and can be found here:</p><ul><li><a href="https://aec.gov.au/Electorates/gis/gis_datadownload.htm">Federal electoral boundary GIS data for free download</a></li><li><a href="https://results.aec.gov.au/24310/Website/HouseDivisionalResults-24310.htm">Divisional results</a></li></ul><h3>Plotting a shape file</h3><p>Using readShapeSpatial() from the maptools package, we pass the shape file in as it’s ownly required parameter and assign it to a variable. Use the plot() function to see how it looks.</p><pre>vicmapdata &lt;- readShapeSpatial(&quot;.\\vic-july-2018-esri\\E_AUGFN3_region.shp&quot;)<br>plot(vicmapdata)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aJUt6WAun19BkY9202iYDA.png" /></figure><p>Read the election data and pass it into a variable</p><pre>electiondata &lt;- read.csv(&quot;.\\vic_elect_results.csv&quot;)</pre><h3>Data Wrangling</h3><p>First we take a look at the structure of the data</p><pre>summary(electiondata)</pre><pre>##      Division  State                  Successful.Party   TCP.Margin<br>##  Aston   : 1   VIC:38   Australian Labor Party:21      1,090  : 1  <br>##  Ballarat: 1            Independent           : 1      10,934 : 1  <br>##  Bendigo : 1            Liberal               :12      11,289 : 1  <br>##  Bruce   : 1            The Greens            : 1      11,326 : 1  <br>##  Calwell : 1            The Nationals         : 3      12,134 : 1  <br>##  Casey   : 1                                           12,453 : 1  <br>##  (Other) :32                                           (Other):32</pre><p>First of all, I don’t like the names of the variables.</p><pre>colnames(electiondata) &lt;- c(&quot;Divisions&quot;, &quot;State&quot;, &quot;Party&quot;, &quot;Marginal Votes&quot;)</pre><pre>summary(electiondata)</pre><pre>##     Divisions  State                       Party    Marginal Votes<br>##  Aston   : 1   VIC:38   Australian Labor Party:21   1,090  : 1    <br>##  Ballarat: 1            Independent           : 1   10,934 : 1    <br>##  Bendigo : 1            Liberal               :12   11,289 : 1    <br>##  Bruce   : 1            The Greens            : 1   11,326 : 1    <br>##  Calwell : 1            The Nationals         : 3   12,134 : 1    <br>##  Casey   : 1                                        12,453 : 1    <br>##  (Other) :32                                        (Other):32</pre><p>This is looking better Next we’ll take a look at the Division names in the shapefile data. This is important because the electiondata Divisions have to map exactly to the shapefile Divisions</p><pre>summary(vicmapdata)</pre><pre>## Object of class SpatialPolygonsDataFrame<br>## Coordinates:<br>##         min       max<br>## x 140.96168 149.97668<br>## y -39.15919 -33.98043<br>## Is projected: NA <br>## proj4string : [NA]<br>## Data attributes:<br>##    E_div_numb       <strong><em>Elect_div</em></strong><strong> </strong>    Numccds          Actual      <br>##  Min.   : 1.00   <strong>Aston   : 1</strong>   Min.   :267.0   Min.   :100151  <br>##  1st Qu.:10.25   <strong>Ballarat: 1</strong>   1st Qu.:337.5   1st Qu.:105494  <br>##  Median :19.50   <strong>Bendigo : 1</strong>   Median :354.0   Median :107416  <br>##  Mean   :19.50   <strong>Bruce   : 1</strong>   Mean   :359.1   Mean   :106954  <br>##  3rd Qu.:28.75   <strong>Calwell : 1</strong>   3rd Qu.:387.0   3rd Qu.:109115  <br>##  Max.   :38.00   <strong>Casey   : 1</strong>   Max.   :467.0   Max.   :112265  <br>##                  (Other) :32                                   <br>##    Projected        Total_Popu   Australian   Area_SqKm            Sortname <br>##  Min.   :107381   Min.   :0    Min.   :0    Min.   :   40.46   Aston   : 1  <br>##  1st Qu.:109070   1st Qu.:0    1st Qu.:0    1st Qu.:   80.59   Ballarat: 1  <br>##  Median :109986   Median :0    Median :0    Median :  170.28   Bendigo : 1  <br>##  Mean   :110372   Mean   :0    Mean   :0    Mean   : 5987.93   Bruce   : 1  <br>##  3rd Qu.:111484   3rd Qu.:0    3rd Qu.:0    3rd Qu.: 2619.61   Calwell : 1  <br>##  Max.   :113924   Max.   :0    Max.   :0    Max.   :81962.21   Casey   : 1  <br>##                                                                (Other) :32</pre><p>It looks like the Divisions in the shapefile are under the variable Elect_div</p><pre>vicmapdata$Elect_div</pre><pre>##  [1] Aston       Ballarat    Bendigo     Bruce       Calwell     Casey      <br>##  [7] Chisholm    Cooper      Corangamite Corio       Deakin      Dunkley    <br>## [13] Flinders    Fraser      Gellibrand  Gippsland   Goldstein   Gorton     <br>## [19] Higgins     Holt        Hotham      Indi        Isaacs      Jagajaga   <br>## [25] Kooyong     La Trobe    Lalor       Macnamara   Mallee      Maribyrnong<br>## [31] Mcewen      Melbourne   Menzies     Monash      Nicholls    Scullin    <br>## [37] Wannon      Wills      <br>## 38 Levels: Aston Ballarat Bendigo Bruce Calwell Casey Chisholm ... Wills</pre><p>So far so good. There are 38 levels in both electiondata$Divisions and vicmapdata$Elect_div which is a good start. We can inspect them manually at this size but what if we were doing the whole country, or a much larger one? We need to do this intelligently and have R do the heavy lifting for us.</p><p><strong>The names in both datasets have to match exactly for the 2 datasets to map together properly.</strong></p><pre>setdiff(electiondata$Division, vicmapdata$Elect_div)</pre><pre>## [1] &quot;McEwen&quot;</pre><p>We do have an inconsistency. It’s telling us that “McEwen” is in the first vector, but not in the second. Let’s find the position of “McEwen” in the first vector, and see what is in the corresponding position in the second vector.</p><pre>which(electiondata$Division==&quot;McEwen&quot;)</pre><pre>## [1] 31</pre><p>So it’s at position 31. What’s at position 31 in the other vector?</p><pre>vicmapdata$Elect_div[31]</pre><pre>## [1] Mcewen<br>## 38 Levels: Aston Ballarat Bendigo Bruce Calwell Casey Chisholm ... Wills</pre><p>Ok so it’s small but the first vector has an ‘E’ where the second has an ‘e’. To stay inside the code, and not have to fix this manually (remember this needs to be scalable), we’ll map the data from one vector into the other (now that we know everything else is the same)</p><pre>vicmapdata$Elect_div &lt;- electiondata$Divisions</pre><p>Now let’s run a check like we did before to make sure the values match up</p><pre>setdiff(electiondata$Division, vicmapdata$Elect_div)</pre><pre>## character(0)</pre><p>That’s better!</p><h3>Creating the Visual</h3><p>Here comes the fun bit! We’re going to use leaflet instead of the plot function we used above. We’ll pass it our ‘vicmapdata’. addPolygons will draw the boundaries for us when we set stroke=True We’ll use white lines with a thickness of 1.5</p><pre>leaflet(vicmapdata) %&gt;%<br>  addPolygons(<br>    stroke = TRUE, <br>    color = &#39;White&#39;, <br>    weight = 1.5, <br>    )</pre><p>(View the interactive version at the link at the top of the page)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/673/1*8z3gW_tncrynpxnRKBWs_g.png" /></figure><p>At this point, we have 2 data variables. 1 for the mapping data, which we have just demonstrated. Now we need to map our election data to the plot.</p><p>First we’ll create the labels. This means that when we hover our mouse over an electorate, it will tell us the relevant statistics. We’re using some HTML tools for formatting once the plot is rendered.</p><pre>mylabels &lt;- paste(<br>  &quot;Electorate: &quot;, vicmapdata$Elect_div, &quot;&lt;br/&gt;&quot;,<br>  &quot;Party: &quot;, electiondata$Party, &quot;&lt;br/&gt;&quot;,<br>  &quot;Margin(votes): &quot;, electiondata$`Marginal Votes`<br>) %&gt;%<br>  lapply(htmltools::HTML)</pre><p>Now we’ll plug our mylabels variable into our previous leaflet code chunk</p><pre>leaflet(vicmapdata) %&gt;%<br>  addPolygons(<br>    stroke = TRUE, <br>    color = &#39;White&#39;, <br>    weight = 1.5, <br>    label = mylabels,<br>    labelOptions = labelOptions( <br>      style = list(&quot;font-weight&quot; = &quot;normal&quot;, padding = &quot;3px 8px&quot;), <br>      textsize = &quot;13px&quot;, <br>      direction = &quot;auto&quot;<br>    )<br>    )</pre><p>(View the interactive version at the link at the top of the page)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/659/1*6aE1YcPji64OOpp0G85Yug.png" /></figure><p>So far so good. Now we’ll add some colours. I created a variable called factpal (factor palette). We’re using the colorFactor function from the leaflet library. topo.colors(5) tells the function that we need 5 colours within the topo.colors palette and unique() allows us to assign a colour to a specific level. In this case we want every electorate with the same winning party to be assigned the same colour.</p><pre>factpal &lt;- colorFactor(topo.colors(5), unique(electiondata$Party))</pre><p>And now we insert the factpal function into our leaflet code chunk</p><pre>leaflet(vicmapdata) %&gt;%<br>  addPolygons(<br>    fillColor = ~factpal(electiondata$Party), <br>    stroke = TRUE, <br>    color = &#39;White&#39;, <br>    weight = 1.5, <br>    label = mylabels,<br>    labelOptions = labelOptions( <br>      style = list(&quot;font-weight&quot; = &quot;normal&quot;, padding = &quot;3px 8px&quot;), <br>      textsize = &quot;13px&quot;, <br>      direction = &quot;auto&quot;<br>    )<br>    )</pre><p>(View the interactive version at the link at the top of the page)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/669/1*XXOy4vYxSMwjm8DZAckXYw.png" /></figure><p>Looking better yet</p><p>From here we just need to add some text and a legend.</p><p>Text: We need a title and some reference links to the data</p><p>Legend: Which colours equate to which party</p><p>Here is the code for the text labels. We’re essentially just feeding in HTML</p><pre>htmltitle &lt;- &quot;&lt;h5&gt; How Victoria voted in the 2019 Federal Election | House of Representatives&lt;/h5&gt;&quot;</pre><pre>references &lt;- &quot;&lt;h5&gt;References&lt;/h5&gt;&lt;a target=&#39;_blank&#39; href=&#39;https://results.aec.gov.au/24310/Website/HouseDivisionalResults-24310.htm&#39;&gt;&lt;h5&gt;Election Data&lt;/h5&gt;&lt;/a&gt;&lt;a target=&#39;_blank&#39; href=&#39;https://aec.gov.au/Electorates/gis/gis_datadownload.htm&#39;&gt;&lt;h5&gt;Geometric Data&lt;/h5&gt;&lt;/a&gt;&quot;</pre><p>Now we just plug in our title and reference data from above and add the legend which you can see below under the addLegend function.</p><pre>leaflet(vicmapdata) %&gt;%<br>  addPolygons(<br>    fillColor = ~factpal(electiondata$Party), <br>    stroke = TRUE, <br>    color = &#39;White&#39;, <br>    weight = 1.5, <br>    label = mylabels,<br>    labelOptions = labelOptions( <br>      style = list(&quot;font-weight&quot; = &quot;normal&quot;, padding = &quot;3px 8px&quot;), <br>      textsize = &quot;13px&quot;, <br>      direction = &quot;auto&quot;<br>    )<br>    ) %&gt;%<br>  addLegend( pal=factpal, <br>             values=~electiondata$Party, <br>             opacity=0.3, <br>             title = &quot;Political Party&quot;, <br>             position = &quot;bottomleft&quot; <br>             ) %&gt;%<br>  addControl(html=htmltitle, position = &quot;topright&quot;) %&gt;%<br>  addControl(html=references, position = &quot;bottomright&quot;)</pre><p>(View the interactive version at the link at the top of the page)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/665/1*z1Ujycx2V3dkUdQxRYzI8g.png" /></figure><h3>Possible Continuations</h3><ol><li>Adding a timescale to show how election results have changed over time</li><li>Create another visual to see which electorates changed hands</li><li>Scale up for the whole of Australia</li></ol><h3>If you’re reading this you made it to the end</h3><p>Thank you for reading. See you in the cloud.</p><p>Eamon</p><p>Twitter @ejooco</p><p>Github <a href="https://github.com/ejooco">https://github.com/ejooco</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8515ef83e275" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/r-and-leaflet-to-create-interactive-choropleth-maps-8515ef83e275">R and Leaflet to create interactive choropleth maps.</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>