This article describes how to build an interactive geographic dashboard in Displayr using R. This example tracks the real-time position of military aircraft. To do so, I'll 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: region shading (to indicate the size of the military force per country) and point markers (for the aircraft positions).
Requirements
You will need geographic data from one of the following sources:
- A variable containing geographic data that has been turned into a table
- Geographic data pasted into a table
- Geographic data sourced from an external input, such as a web page. I'm using Wikipedia's data about military equipment by country in this example.
You will also need a source of real-time data that can be extracted from an online source. In this example, I'm using data from ADS-B Exchange.
Method
- From your Displayr document, go to Calculation > Custom Code.
- Paste the following code into the R CODE input area of the object inspector:
library(rvest)
# Reading in the table from wikipedia
page = read_html("https://en.wikipedia.org/wiki/List_of_countries_by_level_of_military_equipment")
# Obtain the peice of the web page that corresponds to the "wikitable" node
military = html_node(page, ".wikitable")
# Convert the html table element into a data frame
military = html_table(military, fill = TRUE)
aircraft <- military[, c("Countries[a]", "Combat aircraft[c][2]")]
colnames(aircraft) <- c("Country", "MilitaryAircraft")
remove.bracket.content <- function(s) {
return(gsub("\\[.+\\]", "", s))
}
aircraft <- data.frame(apply(aircraft, 2, remove.bracket.content))
aircraft$MilitaryAircraft <- as.numeric(gsub(",", "", aircraft$MilitaryAircraft))
# Remove Total row
aircraft$Country = as.character(aircraft$Country)
if("Total" %in% aircraft$Country) {
aircraft = aircraft[-which(aircraft$Country == "Total"), ]
}
row.names(aircraft) = 1:nrow(aircraft)
aircraft
This code should return a table of data that contains the number of military aircraft by country:
3. Next, you will create a table based on real-time data. To gather the information from ADS-B, I constructed a URL to retrieve a JSON object with information about military aircraft (JSON is flexible text format used for data exchange). I then parsed the JSON to create a data.frame, named positions.
From the toolbar, go to Calculation > Custom Code, and paste the following R code was into the R CODE input area of the object inspector:
positions <- structure(list(Id = c(11403877L, 11404648L, 15205888L, 11415472L,
8190061L, 11403831L, 11403873L, 11486253L, 8190060L, 11402673L,
11404844L, 11405239L, 11407996L, 11404840L, 11403785L, 11404339L,
11406178L, 11422686L, 11407772L, 11405278L, 11405977L, 11404120L,
11408485L, 11422149L, 11407546L, 8190141L, 11408513L, 11405301L,
11407960L, 11404274L, 11403793L, 11404671L, 11411652L, 11408141L,
11426664L, 11407934L), Rcvr = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), HasSig = c(FALSE,
FALSE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE,
TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE,
TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE,
FALSE, TRUE, TRUE, TRUE, TRUE), Icao = c("AE0265", "AE0568",
"E80600", "AE2FB0", "7CF86D", "AE0237", "AE0261", "AF442D", "7CF86C",
"ADFDB1", "AE062C", "AE07B7", "AE127C", "AE0628", "AE0209", "AE0433",
"AE0B62", "AE4BDE", "AE119C", "AE07DE", "AE0A99", "AE0358", "AE1465",
"AE49C5", "AE10BA", "7CF8BD", "AE1481", "AE07F5", "AE1258", "AE03F2",
"AE0211", "AE057F", "AE20C4", "AE130D", "AE5B68", "AE123E"),
Bad = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE), Reg = c("57-1436", "86-0011", "CC-AWA", "08-8204",
"A32-426", "61-0315", "61-0310", NA, "A41-209", "91-00108",
"63-8874", "62-3528", "73-1583", "58-0122", "79-0434", "02-5001",
"82-23713", "10-5701", "03-3119/03-3126 !", "93-0604", "81-23614",
"57-1427", "06-6167", "09-9209", "01-0191", "A32-339", "166764",
"97-0043", "166376", "78-23128", "82-0192", "87-0034", "07-7183",
"166514", "169226", "04-4133"), FSeen = c("/Date(1508716047206)/",
"/Date(1508715827376)/", "/Date(1508715454765)/", "/Date(1508714872276)/",
"/Date(1508714832682)/", "/Date(1508714698635)/", "/Date(1508714257991)/",
"/Date(1508714116944)/", "/Date(1508713890911)/", "/Date(1508713861707)/",
"/Date(1508713819707)/", "/Date(1508713710441)/", "/Date(1508713228188)/",
"/Date(1508713195734)/", "/Date(1508712731216)/", "/Date(1508712371713)/",
"/Date(1508712305994)/", "/Date(1508712117321)/", "/Date(1508712105493)/",
"/Date(1508711847757)/", "/Date(1508711728038)/", "/Date(1508710632937)/",
"/Date(1508710562858)/", "/Date(1508710523108)/", "/Date(1508709867948)/",
"/Date(1508709849604)/", "/Date(1508709648102)/", "/Date(1508709165162)/",
"/Date(1508708845317)/", "/Date(1508708456048)/", "/Date(1508707945936)/",
"/Date(1508705441405)/", "/Date(1508704579603)/", "/Date(1508703415174)/",
"/Date(1508699149912)/", "/Date(1508697765324)/"), TSecs = c(19L,
239L, 612L, 1194L, 1234L, 1368L, 1808L, 1949L, 2175L, 2205L,
2247L, 2356L, 2838L, 2871L, 3335L, 3695L, 3760L, 3949L, 3961L,
4219L, 4338L, 5433L, 5504L, 5543L, 6198L, 6217L, 6418L, 6901L,
7221L, 7610L, 8120L, 10625L, 11487L, 12651L, 16916L, 18301L
), CMsgs = c(3L, 117L, 80L, 731L, 535L, 450L, 1094L, 506L,
1380L, 1422L, 1430L, 1487L, 1004L, 1839L, 1952L, 2295L, 1586L,
2483L, 2260L, 1747L, 1634L, 3351L, 2107L, 1923L, 3452L, 3676L,
3954L, 2462L, 2946L, 3780L, 4803L, 3547L, 6087L, 6690L, 6421L,
7310L), Alt = c(13700L, 13400L, 23725L, 30900L, 22025L, 34900L,
37125L, 975L, 30600L, 40000L, 28000L, 39000L, 10850L, 33000L,
6525L, 37000L, -300L, 27000L, 35000L, 31000L, -200L, 37000L,
29008L, 37000L, 1600L, 24000L, 12975L, 500L, 28000L, 23950L,
23900L, 36000L, 31000L, 22000L, 22000L, 35000L), GAlt = c(13910L,
13799L, 23775L, 31030L, 22105L, 35249L, 37164L, 1194L, 30680L,
40361L, 28381L, 39000L, 11010L, 33139L, 6956L, 37301L, 149L,
27549L, 35180L, 31151L, 249L, 37059L, 29047L, 37301L, 1999L,
24169L, 13185L, 651L, 28484L, 24169L, 24240L, 35932L, 31231L,
22139L, 22316L, 35260L), InHg = c(30.129921, 30.3189, 29.9704723,
30.0501976, 30, 30.2687035, 29.958662, 30.1387787, 30, 30.2805119,
30.3011818, 30.088583, 30.0797253, 30.0590553, 30.3513775,
30.2214565, 30.3690948, 30.4694881, 30.1003933, 30.0708656,
30.3690948, 29.97933, 29.958662, 30.2214565, 30.3189, 30.088583,
30.129921, 30.0708656, 30.40419, 30.1387787, 30.2598419,
29.8523617, 30.15059, 30.0590553, 30.2362213, 30.1801186),
AltT = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Lat = c(35.9931, 39.0798,
-32.75583, 33.613998, -26.471466, 37.363105, 43.096479, 31.133194,
-26.906402, 42.60173, 36.098606, 51.417618, 33.639857, 41.706708,
39.191818, 33.212723, 42.411, 45.0595, 33.377256, 39.049507,
42.4669, 43.564282, 35.4921, 45.888403, 39.253502, -34.266083,
32.2142, 33.974615, 39.822418, 38.7109, 36.394363, 40.625998,
35.088501, 36.8838, 47.515683, 40.283188), Long = c(-84.0423,
-75.193901, -71.19342, -115.430771, 152.56221, -123.640913,
-85.10049, -97.595161, 154.447755, -75.760658, -76.0438,
-2.761131, -116.228714, -87.961014, -74.696228, -103.055603,
-71.2949, -66.7618, -94.547891, -101.171021, -71.2836, -80.181885,
-87.990898, -120.197639, -75.453201, 148.818619, -111.089,
-117.624378, -73.588695, -86.3274, -116.890469, 12.502004,
-99.904999, -87.6797, 3.95212, -79.64832), PosTime = c(1508716065238,
1508716065238, 1508716051144, 1508716065238, 1508716066456,
1508716062597, 1508715388921, 1508716037190, 1508714979152,
1508716054878, 1508716063925, 1508716065238, 1508716039644,
1508716022081, 1508716066441, 1508716066441, 1508715780720,
1508714848089, 1508713399423, 1508715042731, 1508716019472,
1508716066441, 1508714936027, 1508714322664, 1508716058519,
1508716066441, 1508716065222, 1508716058519, 1508716066441,
1508715387608, 1508716066441, 1508713721988, 1508712256791,
1508712305900, 1508714958230, 1508716066425), Mlat = c(FALSE,
FALSE, FALSE, TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE,
FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE, TRUE,
FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, TRUE), Tisb = c(FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE),
Trak = c(35.2, 86, 0, 91.6, 285, 51.4, 83.4, 100.3, 70.1,
278.5, 170.9, 85.4, 95.8, 66.6, 38.4, 77.7, 174.6, 60.6,
74.4, 77.7, 179.8, 81.8, 53, 121.2, 185, 161.6, 235.1, 104,
219.9, 276, 343.4, NA, 87, 320.4, 330.6, 86.4), TrkH = c(FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE),
Type = c("K35R", "C5M", "A320", "C17", "B350", "K35R", "K35R",
"H60", "C17", "GLF4", "K35R", "K35R", "C130", "K35R", "DC10",
"B752", "H60", "C30J", "C17", "C17", "H60", "K35R", "C17",
"C17", "C17", "B350", "C30J", "C17", "GLF5", "BE20", "DC10",
"C5M", "C17", "C30J", "C30J", "C17"), Mdl = c("Boeing KC-135R",
"Lockheed C-5M Galaxy", "Airbus A320-232SL", "Boeing C-17A Globemaster III",
"Raytheon Aircraft Company King Air B350", "Boeing KC-135R",
"Boeing KC-135R", "UH-60M Black Hawk", "Boeing C-17A Globemaster III",
"Gulfstream C-20F", "Boeing KC-135R Stratotanker", "Boeing KC-135R",
"Lockheed EC-130H Compass Call", "Boeing KC-135R", "Douglas KC-10A Extender",
"Boeing C-32B", "Sikorsky UH-60A Black Hawk", "Lockheed C-130J-30 Hercules",
"Boeing C-17A Globemaster III", "McDonnell Douglas C-17A Globemaster III",
"Sikorsky UH-60A Black Hawk", "Boeing KC-135R", "Boeing C-17A Globemaster III",
"Boeing C-17A Globemaster III", "Boeing C-17A Globemaster III",
"Raytheon Aircraft Company King Air B350", "Lockheed KC-130J Hercules",
"McDonnell Douglas C-17A Globemaster III", "Gulfstream Aerospace C-37B",
"Beech C-12C Huron", "Douglas KC-10A Extender", "Lockheed C-5M Galaxy",
"Boeing C-17A Globemaster III", "Lockheed KC-130J Hercules",
"KC-130J", "Boeing C-17A Globemaster III"), Man = c("Boeing",
"Lockheed", "Airbus", "Boeing", "Raytheon Aircraft Company",
"Boeing", "Boeing", "Sikorsky", "Boeing", "Gulfstream Aerospace",
"Boeing", "Boeing", "Lockheed", "Boeing", "Douglas", "Boeing",
"Sikorsky", "Lockheed", "Boeing", "McDonnell Douglas", "Sikorsky",
"Boeing", "Boeing", "Boeing", "Boeing", "Raytheon Aircraft Company",
"Lockheed", "McDonnell Douglas", "Gulfstream Aerospace",
"Beech", "Douglas", "LOCKHEED", "Boeing", "Lockheed", "Lockheed",
"Boeing"), CNum = c("17507", "500-0097", "7595", "P204",
"FL-426", "18222", "18217", NA, "AU4", "1162", "18722", "18511",
"4545", "17867", "48201", NA, NA, "5701", "P119", "P20",
NA, "17498", "P167", "P209", "P91", "FL-339", "5564", "P43",
"5041", "BC-64", "48214", "500-0120", "P183", "5556", "5798",
"P133"), Op = c("United States Air Force", "United States Air Force",
"JetSMART", "United States Air Force", "Royal Australian Air Force",
"United States Air Force", "United States Air Force", "United States Army Louisiana Army National Guard",
"Royal Australian Air Force", "United States Army ", "United States Air Force",
"United States Air Force", "United States Air Force", "United States Air Force",
"United States Air Force", "United States Air Force", "United States Army",
"United States Air Force", "United States Air Force", "United States Air Force",
"United States Army", "United States Air Force", "United States Air Force",
"United States Air Force", "United States Air Force", "Royal Australian Air Force",
"United States Marine Corps", "United States Air Force",
"United States Navy", "United States Army", "United States Air Force",
"United States Air Force", "United States Air Force", "United States Marine Corps",
"United States Marine Corps VMGR-352", "United States Air Force"
), OpIcao = c("RCH", "USAF", NA, "USAF", "ASY", "USAF", "RCH",
NA, "ASY", "USARMY", "USAF", "RCH", "USAF", "RCH", "RCH",
"USAF", NA, "RCH", "RCH", "RCH", NA, "RCH", "USAF", "RCH",
"USAF", "ASY", "USMC", "USAF", "USN", "USARMY", "USAF", "USAF",
"USAF", NA, NA, "USAF"), Sqk = c("", "5654", "2201", "7374",
"7301", "2443", "1313", "", "1004", "3536", "1616", "4744",
"1012", "1711", "7331", "4115", "5173", "7330", "3357", "1450",
"1200", "1720", "6221", "6665", "2205", "1045", "1503", "1303",
"2063", "2207", "2313", "4033", "7751", "3324", "5712", "7721"
), VsiT = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L,
0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L,
0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), WTC = c(3L, 3L, 2L,
3L, 2L, 3L, 3L, 2L, 3L, 2L, 3L, 3L, 2L, 3L, 3L, 2L, 2L, 2L,
3L, 3L, 2L, 3L, 3L, 3L, 3L, 2L, 2L, 3L, 2L, 1L, 3L, 3L, 3L,
2L, 2L, 3L), Species = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 1L, 1L, 4L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Engines = c("4",
"4", "2", "4", "2", "4", "4", "2", "4", "2", "4", "4", "4",
"4", "3", "2", "2", "4", "4", "4", "2", "4", "4", "4", "4",
"2", "4", "4", "2", "2", "3", "4", "4", "4", "4", "4"), EngType = c(3L,
3L, 3L, 3L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 3L,
2L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 3L,
3L, 3L, 2L, 2L, 3L), EngMount = c(0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), Mil = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE), Cou = c("United States",
"United States", "Chile", "United States", "Australia", "United States",
"United States", "United States", "Australia", "United States",
"United States", "United States", "United States", "United States",
"United States", "United States", "United States", "United States",
"United States", "United States", "United States", "United States",
"United States", "United States", "United States", "Australia",
"United States", "United States", "United States", "United States",
"United States", "United States", "United States", "United States",
"United States", "United States"), HasPic = c(FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), Interested = c(FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE),
FlightsCount = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), SpdTyp = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L), CallSus = c(FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE), Trt = c(2L, 2L, 2L, 2L,
4L, 2L, 2L, 4L, 2L, 4L, 2L, 2L, 2L, 2L, 4L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 4L, 2L, 2L, 5L, 2L, 2L, 2L, 2L, 2L,
2L, 2L), Spd = c(NA, 313, 430, 473.7, 181, NA, 480.4, 89.4,
NA, 401, 488, NA, 270.4, 524.3, 289, 461.5, NA, NA, NA, 460.8,
NA, 518.3, 516, 541.8, 139, 263, NA, 279.2, 450.8, 234, 489.4,
NA, 455, 323, 242.1, 485), Gnd = c(NA, FALSE, NA, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE), Call = c(NA, "RCH175",
"JAT032", NA, "DNGO31", "PISTN61", "RCH558", "20533", "ASY718",
"PAT108", "SPUR71", "RCH793", "ZAPER01", "RCH561", "HOIST90",
"ILIAD87", "8223713", "RCH519", "RCH576T", "RHINO90", "08123614",
"RCH566", "RCH037", "RCH538", "RIDER69", "BFRT10", "OTIS27",
"SLAM99", "VM101", NA, "HORSE61", "RCH181", "VOLT73", "OTIS43",
"SHIVA03", "JEDI61"), Help = c(NA, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, NA, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE), Vsi = c(NA, 3300L, 1530L, 1024L,
64L, NA, 1152L, -320L, NA, 0L, 64L, NA, -64L, NA, -1024L,
0L, NA, NA, NA, -1536L, NA, 128L, 45L, 1024L, 0L, 0L, NA,
-1600L, 0L, 9L, -64L, NA, 25L, 21L, 0L, 38L), Sig = c(NA,
NA, NA, 139L, NA, 40L, 33L, 33L, 90L, NA, NA, 59L, NA, 184L,
NA, 146L, 20L, 95L, NA, 30L, 18L, 86L, 29L, 121L, NA, NA,
96L, 39L, NA, 31L, 19L, NA, 201L, 48L, 55L, 64L), Year = c(NA,
NA, "2017", NA, NA, NA, NA, NA, "2007", "1991", NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, "2007", NA, NA, NA, NA,
NA, "2004", NA, NA, "1987", NA, NA, "2016", NA), PosStale = c(NA,
NA, NA, NA, NA, NA, TRUE, NA, TRUE, NA, NA, NA, NA, NA, NA,
NA, TRUE, TRUE, TRUE, TRUE, NA, NA, TRUE, TRUE, NA, NA, NA,
NA, NA, TRUE, NA, TRUE, TRUE, TRUE, TRUE, NA), TTrk = c(NA,
NA, NA, NA, 235, NA, NA, NA, NA, NA, NA, NA, NA, NA, 55,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
331, NA, NA, NA, NA, NA), Tag = c(NA, NA, NA, NA, NA, NA,
NA, NA, NA, "DOD", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "DOD", "USAF", NA,
NA, NA, NA), TAlt = c(NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 25000L, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 28000L, NA, 24000L, NA, NA, NA, NA, NA
), From = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "KMYR Myrtle Beach, United States",
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), To = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, "KNHK Patuxent River Naval Air Station/Trapnell Field Aiport, United States",
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), row.names = c("1",
"4", "7", "9", "11", "12", "13", "14", "15", "16", "17", "18",
"19", "20", "22", "23", "24", "25", "26", "27", "28", "29", "30",
"31", "32", "33", "34", "35", "37", "38", "40", "41", "43", "44",
"48", "49"), class = "data.frame")
And the output table appears as:
4. Now, we'll create a plotly map of the world. The countries are shaded according to the size of the air force, with the scale shown on a legend. In plotly terminology, each layer of the map is known as a trace.
From the toolbar, go to Calculation > Custom Code and paste the following R code was into the R CODE input area of the object inspector:
library(plotly)
library(flipFormat)
# specify map area and projection
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) # higher resolution
# shade countries by airforce size
p <- plot_geo(aircraft) %>%
add_trace(data = aircraft, 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')
Note that I am calling in the table aircraft in various points, which was created in Step 2 above. You should now have a shaded geographic map that shows aircraft density by country:
5. And, finally, I've added markers showing the airplane locations as another trace. I use a different color for those with speeds less than 200 knots and altitudes less than 2000 feet. The hover text contains more detail about the aircraft.
From the toolbar, go to Calculation > Custom Code, and paste the following R code was into the R CODE input area of the object inspector:
if (is.data.frame(positions) && nrow(positions) > 0) {
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)
}
In the code above, I've called in positions from the table created in Step 3 and overlayed that onto the geographic density map (p) created in Step 4.
You should now see a geographic map that shows aircraft density by country, along with publicly available real-time data of military aircraft in-flight. You can hover over one of the dots for information about the aircraft operator, model, latitude, longitude, and altitude.
Next
How to Create Dashboards with Real-Time Updating