The What

In this tutorial, we are going to combine the power of 3 R packages to create a nice visual for countries by their respective continents and their capitals.

Packages used

Load packages

quickcode::libraryAll(
  r2country,
  collapsibleTree,
  quietly = TRUE,
  clear = TRUE
)
## Warning: package 'collapsibleTree' was built under R version 4.0.5
## Packages Loaded:
## r2country, version 2.0.2.3.1
## collapsibleTree, version 0.1.7

Prepare and previous continents and country data from r2country package

# use datasets of continent, country names, and capitals

final.dt <- country_names

for (cl in c(
  "country_continent", # continents
  "country_capital", # capitals
  "country_money", # currencies
  "country_population" # population
)) {
  final.dt <- merge(final.dt, get(cl), by = "ID")
}


# preview the dataset
names(final.dt)
## [1] "ID"              "name"            "continent"       "capital"        
## [5] "currency"        "symbol"          "isocode"         "fractionalunity"
## [9] "population2023"
head(final.dt)
##      ID                name     continent          capital
## 1 10001         Afghanistan          Asia            Kabul
## 2 10002             Albania        Europe           Tirana
## 3 10003             Algeria        Africa          Algiers
## 4 10004             Andorra        Europe Andorra la Vella
## 5 10005              Angola        Africa           Luanda
## 6 10006 Antigua and Barbuda North America     Saint John's
##                   currency           symbol isocode fractionalunity
## 1           Afghan afghani <U+060B><U+200E>     AFN             Pul
## 2             Albanian lek              Lek     ALL          Qintar
## 3           Algerian dinar               DA     DZD         Santeem
## 4                     Euro                \200     EUR            Cent
## 5           Angolan kwanza               Kz     AOA         Cêntimo
## 6 Eastern Caribbean dollar                $     XCD            Cent
##   population2023
## 1       34262840
## 2        2761785
## 3       45400000
## 4          83523
## 5       33086278
## 6         100772

Visualize the country dataset


Continent ⇉ Country ⇉ Captitals

collapsibleTree(
  final.dt,
  hierarchy = c("continent", "name", "capital"),
  root = "Continents",
  width = 1800,
  fontSize = 15,
  height = 1000,
  zoomable = TRUE
)

Continent ⇉ Country ⇉ Currency

collapsibleTree(
  final.dt,
  hierarchy = c("continent", "name", "currency"),
  root = "Continents",
  width = 1800,
  fontSize = 15,
  height = 1000,
  zoomable = TRUE
)
## Warning in Climb(mynode, path): unable to translate 'Vietnamese d<U+1ED3>ng' to
## native encoding
## Warning in self[[child$name]] <- child: unable to translate 'Vietnamese
## d<U+1ED3>ng' to native encoding

Continent ⇉ Country ⇉ Population

collapsibleTree(
  final.dt,
  hierarchy = c("continent", "name", "population2023"),
  root = "Continents",
  width = 1800,
  fontSize = 15,
  height = 1000,
  zoomable = TRUE
)

Continent ⇉ Country ⇉ isocode

collapsibleTree(
  final.dt,
  hierarchy = c("continent", "name", "isocode"),
  root = "Continents",
  width = 1800,
  fontSize = 15,
  height = 1000,
  zoomable = TRUE
)