R Visualize countries, currencies and population of the world using r2country"

knitr::opts_chunk$set(echo = TRUE)

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
)

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)
head(final.dt)

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
)

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
)

R package links



Try the r2country package in your browser

Any scripts or data that you put into this service are public.

r2country documentation built on Sept. 11, 2024, 9:24 p.m.