In this post, I’ll show you how to build an interactive geographic observation panel with
Displayr ,
Plotly and
R. It is particularly interesting that it tracks the position of military aircraft in real time. For this, I am going to take data from two different sources (regions based on the size of the Air Force and tracking the position of the aircraft in real time). The Observation Panel displays dynamic data in two ways: a
regional hue (to show the number of air forces in the country) and
marker points (for aircraft positions). Then I will build a map to accurately and beautifully display all this data.
Reading tabular data from the network
I am going to paint every country on the map according to the size of its air force. For this you need a list of aircraft for each country. Fortunately, Wikipedia is always at our service, and there is exactly what you need (
here ). The code below reads the data and cleans it up for presentation in a convenient table.
library(httr) library(XML) # URL url <- "https://en.wikipedia.org/wiki/List_of_countries_by_level_of_military_equipment#List" r <- GET(url) airforces <- readHTMLTable(doc = content(r, "text"))[[2]] # airforces <- airforces[-1, c("Country[note 1]", "Military aircraft[note 3]")] colnames(airforces) <- c("Country", "MilitaryAircraft") remove.bracket.content <- function(s) { return(gsub("\\[.+\\]", "", s)) } airforces <- data.frame(apply(airforces, 2, remove.bracket.content)) airforces$MilitaryAircraft <- as.numeric(gsub(",", "", airforces$MilitaryAircraft)) airforces
Real-time data pooling from all over the world
Compared to what is higher, the second data source is more dynamic. I use
ADS-B , which provide real-time information about flights across the planet. Not all military operations are top secret. In fact, some military aircraft transmit their position in the public domain.
To map this information, I
build a URL to get a JSON object with information about a military aircraft (JSON is a flexible text format for exchanging data). Then I read JSON in
data.frame .
')
library(jsonlite) url <- "http://public-api.adsbexchange.com/VirtualRadar/AircraftList.json?" url <- paste0(url, "fMilQ=TRUE") positions <- fromJSON(url)$acList if (length(positions) != 0) { positions <- positions[positions$Type != "TEST", ] positions <- positions[!is.na(positions$Lat), ] } positions
Coloring countries on the map
The code below creates a
plotly map of the world. Countries are painted according to the size of the Air Force, the scale is shown in the legend. In
plotly terminology
, each map layer is called a
trace .
library(plotly) library(flipFormat) # g <- list(scope = "world", showframe = FALSE, showcoastlines = TRUE, projection = list(type = 'mercator'), lonaxis = list(range = c(-140, 179)), lataxis = list(range = c(-55, 70)), resolution = 50) # # p <- plot_geo(airforces) add_trace(data = airforces, name = "Airforce", z = ~MilitaryAircraft, color = ~MilitaryAircraft, colors = 'Blues', locations = ~Country, marker = list(line = list(color = toRGB("grey"), width = 0.5)), showscale = TRUE, locationmode = "country names", colorbar = list(title = 'Airforce', separatethousands = TRUE)) config(displayModeBar = F) layout(geo = g, margin = list(l=0, r=0, t=0, b=0, pad=0), paper_bgcolor = 'transparent')
Adding markers for aircraft
Finally, we add markers showing the positions of the planes as another trace. I use different colors for those with a speed of less than 200 knots and a height of less than 610 meters. More information about aircraft in the
tooltips .
aircolors = rep("airborne", nrow(positions)) # aircolors[positions$Spd < 200 & positions$Alt < 2000] <- "ground/approach" hovertext = paste0("Operator:", positions$Op, "\nModel:", positions$Mdl, "\nAltitide(ft):", sapply(positions$Alt, FormatAsReal)) hoverinfo = rep("all", nrow(positions)) p = add_trace(p, data = positions, x = positions$Long, y = positions$Lat, color = aircolors, hovertext = hovertext, showlegend = FALSE)
Here is the final result .
Few finishing touches
Although the map above shows everything you need, it is easy to make it more comfortable and beautiful.
Displayr allows
you to
add a control to switch between textual and graphic annotations and background. Here is a
link to the final version of the monitoring panel and a screenshot below.
