<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/how-to-build-a-geographic-dashboard-with-real-time-data/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.displayr.com/how-to-build-a-geographic-dashboard-with-dynamic-real-time-data/?utm_medium=Feed&utm_source=Syndication"> R – Displayr</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In this post, I show how to build an interactive geographic dashboard using <em>Displayr</em>, <em>Plotly</em> and <em>R</em>. It is particularly fascinating in that it tracks the real-time position of military aircraft. To do so, I am first going to bring in some data from two separate sources (regions based on the size of military air-force, and then the real-time tracking of aircraft positions). The dashboard displays the dynamic data in two ways: <em>region shading </em>(to indicate the size of the military force per country) and <em>point markers</em> (for the aircraft positions). I then build a map to neatly and attractively display all this data.</p>
<hr>
<h2><span>Reading tabular data from the web</span></h2>
<p>I am going to shade each country of the map according to the size of its military air-force. To do this I need a list of aircraft per country. Fortunately, the ever useful Wikipedia has just what I need (<a rel="nofollow" href="https://en.wikipedia.org/wiki/List_of_countries_by_level_of_military_equipment#List" target="_blank">here</a>). The following code reads in the data and cleans it to produce a neat table.</p>
<pre>
library(httr)
library(XML)

# Extract table via URL
url &amp;amp;amp;lt;- &quot;https://en.wikipedia.org/wiki/List_of_countries_by_level_of_military_equipment#List&quot;
r &amp;amp;amp;lt;- GET(url)
airforces &amp;amp;amp;lt;- readHTMLTable(doc = content(r, &quot;text&quot;))[[2]]

# Clean required columns
airforces &amp;amp;amp;lt;- airforces[-1, c(&quot;Country[note 1]&quot;, &quot;Military aircraft[note 3]&quot;)]
colnames(airforces) &amp;amp;amp;lt;- c(&quot;Country&quot;, &quot;MilitaryAircraft&quot;)
remove.bracket.content &amp;amp;amp;lt;- function(s) {
return(gsub(&quot;\\[.+\\]&quot;, &quot;&quot;, s))
}
airforces &amp;amp;amp;lt;- data.frame(apply(airforces, 2, remove.bracket.content))
airforces$MilitaryAircraft &amp;amp;amp;lt;- as.numeric(gsub(&quot;,&quot;, &quot;&quot;, airforces$MilitaryAircraft))
airforces
</pre>
<hr>
<h2><span>Pooling real-time data from across the globe</span></h2>
<p>Compared to the above, the second source of data is more dynamic. I am using <a rel="nofollow" href="https://adsbexchange.com/" target="_blank">ADS-B</a>, which pools real-time flight tracking information from across the globe. Not all military operations are top secret. Apparently, some aircraft operated by the armed forces broadcast their positions publicly.</p>
<p>To collate this information, I <a rel="nofollow" href="http://www.virtualradarserver.co.uk/Documentation/Formats/AircraftList.aspx" target="_blank">construct a URL</a> to retrieve a JSON object with information about military aircraft (JSON is flexible text format used for data exchange). Then I parse the JSON to create a <em>data.frame</em>.</p>
<pre>
library(jsonlite)&amp;amp;amp;lt;/pre&amp;amp;amp;gt;
url &amp;amp;amp;lt;- &quot;http://public-api.adsbexchange.com/VirtualRadar/AircraftList.json?&quot;
url &amp;amp;amp;lt;- paste0(url, &quot;fMilQ=TRUE&quot;)

positions &amp;amp;amp;lt;- fromJSON(url)$acList

if (length(positions) != 0) {
positions &amp;amp;amp;lt;- positions[positions$Type != &quot;TEST&quot;, ]
positions &amp;amp;amp;lt;- positions[!is.na(positions$Lat), ]
}
positions
</pre>
<hr>
<h2><span>Shading countries of a map</span></h2>
<p>The following code produces a <a rel="nofollow" href="https://plot.ly/r/" target="_blank"><em>plotly</em></a> map of the world. The countries are shaded according to the size of the air-force, with the scale shown on a legend. In <em>plotly</em> terminology, each layer of the map is known as a <em>trace. </em></p>
<pre>
library(plotly)
library(flipFormat)

# specify map area and projection
g &amp;amp;amp;lt;- list(scope = &quot;world&quot;,
showframe = FALSE, showcoastlines = TRUE,
projection = list(type = 'mercator'),
lonaxis = list(range = c(-140, 179)),
lataxis = list(range = c(-55, 70)),
resolution = 50) # higher resolution

# shade countries by airforce size
p &amp;amp;amp;lt;- plot_geo(airforces) %&amp;amp;amp;gt;%

add_trace(data = airforces, name = &quot;Airforce&quot;,
z = ~MilitaryAircraft, color = ~MilitaryAircraft,
colors = 'Blues', locations = ~Country,
marker = list(line = list(color = toRGB(&quot;grey&quot;), width = 0.5)),
showscale = TRUE, locationmode = &quot;country names&quot;,
colorbar = list(title = 'Airforce', separatethousands = TRUE)) %&amp;amp;amp;gt;%
config(displayModeBar = F) %&amp;amp;amp;gt;%

layout(geo = g,
margin = list(l=0, r=0, t=0, b=0, pad=0),
paper_bgcolor = 'transparent')
</pre>
<hr>
<h2><span>Adding markers for the aircraft</span></h2>
<p>Finally, I add markers showing the airplane locations as another trace. I use a different color for those with speed less than 200 knots and altitude less than 2000 feet. The <em>hover text</em> contains more detail about the aircraft.</p>
<pre>
aircolors = rep(&quot;airborne&quot;, nrow(positions))  # create a vector with the status of each aeroplane
aircolors[positions$Spd &amp;amp;amp;lt; 200 &amp;amp;amp;amp; positions$Alt &amp;amp;amp;lt; 2000] &amp;amp;amp;lt;- &quot;ground/approach&quot;
hovertext = paste0(&quot;Operator:&quot;, positions$Op, &quot;\nModel:&quot;, positions$Mdl, 
&quot;\nAltitide(ft):&quot;, sapply(positions$Alt, FormatAsReal))
hoverinfo = rep(&quot;all&quot;, nrow(positions))

p = add_trace(p, data = positions, x = positions$Long, y = positions$Lat, 
 color = aircolors, hovertext = hovertext, showlegend = FALSE)
</pre>
<p>The final result is shown below. Since broadcasting of position is purely voluntary, I suspect that the <a rel="nofollow" href="https://www.youtube.com/watch?v=vwBbrngafl0" target="_blank">Top Gun</a> flights are not shown!</p>
<p><iframe width="450" src="https://embedded.azureedge.net/75668/233059/11c70bd3-724f-43b8-ac99-f00b37a5d593.html?v=29692-7c5e1a8cf9-903562984"></iframe>
</p>
<hr>
<h2><span>Adding some finishing touches</span></h2>
<p>While the map above shows the required information, it can easily be made more useful and appealing. <a rel="nofollow" href="http://www.displayr.com/" target="_blank">Displayr</a> allows me to <a rel="nofollow" href="https://www.displayr.com/combo-box/" target="_blank">add a control</a> to switch between regions, text and image annotations, and a background. Here is <a rel="nofollow" href="https://app.displayr.com/Dashboard?id=8722ec16-5b6d-4951-bbdb-99b11c8e04f1" target="_blank">a link to the finished dashboard</a>, with a screenshot shown below.</p>
<p><img sizes="(max-width: 700px) 100vw, 700px" src="https://i1.wp.com/www.displayr.com/wp-content/uploads/2017/10/Geographic_screenshot.png?w=450&ssl=1" alt="Militiary aircraft still of the map" class="wp-image-3920" data-recalc-dims="1"></p>
<p>Thinking about it again, if you <em>can’t </em>see any military aircraft at all on the radar, then you should really worry!</p>
<hr>
<h2>Try it yourself</h2>
<p>You can explore the dashboards used in this post within Displayr. All the code is embedded within the pages (look on the right-hand panel after you click on an object). Click <a rel="nofollow" href="https://app.displayr.com/Home/SelectCompany?redir=%2FTry%2FGeographic+Dashboard+World" target="_blank">here </a>to see the copy of the working document (without background and finishing touches). Click <a rel="nofollow" href="https://app.displayr.com/Home/SelectCompany?redir=%2FTry%2FMilitary+Aircraft" target="_blank">here</a> open a copy of the finished document that includes the background, control box, and other finishing touches.</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://www.displayr.com/how-to-build-a-geographic-dashboard-with-dynamic-real-time-data/?utm_medium=Feed&utm_source=Syndication"> R – Displayr</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 width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/aY1WbH2Xjhc" height="1"></a></div>