Click the Original, Code and Reconstruction tabs to read about the issues and how they were fixed.

Original


Source: Worldpopulationreview.com. 2022. Military Size by Country 2022. [online] Available at: https://worldpopulationreview.com/country-rankings/military-size-by-country [Accessed 25 April 2022].


Objective

Explain the objective of the original data visualisation and the targetted audience.

The visualisation chosen had the following three main issues:

  • It is hard to differentiate between the colours chosen, e.g. 9M, 10M, and 11M all look very similar.
  • Some of the colours used in the map can’t be found in the scale.
  • The legend indicates that a colour is associated with a specific value, rather than the range of values which the map depicts.

Reference

Code

The following code was used to fix the issues identified in the original.

library(ggplot2)
library(maps)
library(dplyr)
library(rgeos)
library(maptools)
library(ggmap)
library(broom)
library(stringr)

## There were some country names that didn't match between the two files.
## I edited these manually in excel.

military <- read.csv('military_data.csv', fileEncoding="UTF-8-BOM")

world <- map_data("world")

shp <- readShapeSpatial('shp/world-administrative-boundaries.shp')

library(leaflet)

military.leaf <- military %>%
  rename(name = country)

merged<-sp::merge(shp, military.leaf, by='name', duplicateGeoms = TRUE)

bins = c(0,100000,500000,1000000,5000000,11000000)
bins <- as.integer(bins)

pal <- colorBin(
  "YlOrBr",
  domain = military.leaf$total, 
  bins = bins,
  pretty = FALSE
  )

labels <- sprintf(
  "<strong>%s</strong><br/>
  %s Active Duty <br>
  %s Paramilitary <br>
  %s Reserves <br>
  %s Total Military Personnel <br>
  %s Total Military Personnel Per 1000 Capita",
  merged$name,
  prettyNum(merged$activeDuty,big.mark=","),
  prettyNum(merged$paramilitary,big.mark=","),
  prettyNum(merged$reserves,big.mark=","),
  prettyNum(merged$total,big.mark=","),
  prettyNum(round(merged$total/(merged$pop2022),0),big.mark=",")
) %>% lapply(htmltools::HTML)

library(htmlwidgets)
library(htmltools)

title <- tags$div(
   HTML('<h3>Total Military Personnel by Country - 2022</h3>')
 )

p3 <- leaflet(merged, options = leafletOptions(zoomSnap=0.25)) %>%
  setView(lng = 0, lat = 35, zoom = 1.25)
p3 <- p3 %>% addPolygons(
  fillColor = ~pal(total),
  weight = 1,
  opacity = 1,
  color = "#444444",
  dashArray = "1",
  fillOpacity = 0.7,
  highlight = highlightOptions(
    weight = 3,
    color = "white",
    dashArray = "",
    fillOpacity = 0.7,
    bringToFront = TRUE),
      label = labels,
  labelOptions = labelOptions(
    style = list("font-weight" = "normal", padding = "3px 8px"),
    textsize = "15px",
    direction = "auto")) %>% 
  addLegend(pal = pal, 
            values = ~total, 
            opacity = 0.7, title = "Total Military Personnel",
  position = "bottomleft") %>% 
  addControl(title, position = "topright")

Data Reference

Reconstruction

The following plot fixes the main issues in the original.