You should use this code as part of your Visualization 3 assignment. You will re-save this Notebook under a different name and modify it to complete the assignment. For example, you should delete all of the text in this section and replace it with your own introduction.
The first chunk calls the packages that you will need in this assignment.
The second code chunk automatically retrieves the latest data from the World Development Indicators database, for use in the assignment.
library(tidyverse)
library(leaflet)
library(WDI)
WDI to retrieve most updated figures available.In this assignment, we will fetch ten data series from the WDI:
| Tableau Name | WDI Series |
|---|---|
| Birth Rate | SP.DYN.CBRT.IN |
| Infant Mortality Rate | SP.DYN.IMRT.IN |
| Internet Usage | IT.NET.USER.ZS |
| Life Expectancy (Total) | SP.DYN.LE00.IN |
| Forest Area (% of land) | AG.LND.FRST.ZS |
| Mobile Phone Usage | IT.CEL.SETS.P2 |
| Population Total | SP.POP.TOTL |
| International Tourism receipts (current US$) | ST.INT.RCPT.CD |
| Import value index (2000=100) | TM.VAL.MRCH.XD.WD |
| Export value index (2000=100) | TX.VAL.MRCH.XD.WD |
The next code chunk will call the WDI API and fetch the years 1998 through 2018, as available. You will find that only a few variables have data for 2018. The dataframe will also contain the longitude and latitude of the capital city in each country.
Note This notebook will take approximately 2 minutes to run. The WDI call is time-consuming as is the process of knitting the file. Be patient.
The World Bank uses a complex, non-intuitive scheme for naming variables. For example, the Birth Rate series is called SP.DYN.CBRT,IN. The code assigns variables names that are more intuitive than the codes assigned by the World Bank, and converts the geocodes from factors to numbers.
In your code, you will use the data frame called countries.
birth <- "SP.DYN.CBRT.IN"
infmort <- "SP.DYN.IMRT.IN"
net <-"IT.NET.USER.ZS"
lifeexp <- "SP.DYN.LE00.IN"
forest <- "AG.LND.FRST.ZS"
mobile <- "IT.CEL.SETS.P2"
pop <- "SP.POP.TOTL"
tour <- "ST.INT.RCPT.CD"
import <- "TM.VAL.MRCH.XD.WD"
export <- "TX.VAL.MRCH.XD.WD"
# create a vector of the desired indicator series
indicators <- c(birth, infmort, net, lifeexp, forest,
mobile, pop, tour, import, export)
countries <- WDI(country="all", indicator = indicators,
start = 1998, end = 2018, extra = TRUE)
## rename columns for each of reference
countries <- rename(countries, birth = SP.DYN.CBRT.IN,
infmort = SP.DYN.IMRT.IN, net = IT.NET.USER.ZS,
lifeexp = SP.DYN.LE00.IN, forest = AG.LND.FRST.ZS,
mobile = IT.CEL.SETS.P2, pop = SP.POP.TOTL,
tour = ST.INT.RCPT.CD, import = TM.VAL.MRCH.XD.WD,
export = TX.VAL.MRCH.XD.WD)
# convert geocodes from factors into numerics
countries$lng <- as.numeric(as.character(countries$longitude))
countries$lat <- as.numeric(as.character(countries$latitude))
# Remove groupings, which have no geocodes
countries <- countries %>%
filter(!is.na(lng))
glimpse(countries)
## Observations: 4,368
## Variables: 22
## $ iso2c <chr> "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD"...
## $ country <chr> "Andorra", "Andorra", "Andorra", "Andorra", "Andorra...
## $ year <dbl> 2005, 2003, 2006, 2018, 2008, 2004, 2007, 2011, 2017...
## $ birth <dbl> 10.700, 10.300, 10.600, NA, 10.400, 10.900, 10.100, ...
## $ infmort <dbl> 4.1, 4.3, 4.1, NA, 3.9, 4.2, 4.0, 3.7, 3.2, 3.6, 3.5...
## $ net <dbl> 37.605766, 13.546413, 48.936847, NA, 70.040000, 26.8...
## $ lifeexp <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ...
## $ forest <dbl> 34.042553, 34.042553, 34.042553, NA, 34.042553, 34.0...
## $ mobile <dbl> 81.85933, 70.90951, 85.19959, NA, 76.55764, 76.55160...
## $ pop <dbl> 78867, 73182, 80991, NA, 83861, 76244, 82683, 83751,...
## $ tour <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ...
## $ import <dbl> 178.0635, 149.5233, 176.6679, NA, 191.7003, 174.0925...
## $ export <dbl> 314.89205, 198.89232, 364.84322, NA, 306.86583, 271....
## $ iso3c <fct> AND, AND, AND, AND, AND, AND, AND, AND, AND, AND, AN...
## $ region <fct> Europe & Central Asia, Europe & Central Asia, Europe...
## $ capital <fct> Andorra la Vella, Andorra la Vella, Andorra la Vella...
## $ longitude <fct> 1.5218, 1.5218, 1.5218, 1.5218, 1.5218, 1.5218, 1.52...
## $ latitude <fct> 42.5075, 42.5075, 42.5075, 42.5075, 42.5075, 42.5075...
## $ income <fct> High income, High income, High income, High income, ...
## $ lending <fct> Not classified, Not classified, Not classified, Not ...
## $ lng <dbl> 1.5218, 1.5218, 1.5218, 1.5218, 1.5218, 1.5218, 1.52...
## $ lat <dbl> 42.5075, 42.5075, 42.5075, 42.5075, 42.5075, 42.5075...
Beyond this line, you will insert your original code, following the instructions in the assignment.
Replace this text chunk with an explanation of what you have done, and what differences you notice between this viz and your work in Assignment 2
# your code goes here
Replace this text chunk with an explanation of what you have done, and what you notice in this map.
# your code goes here
Replace this text chunk with an explanation of what you have done, and what you notice between 1998 and the recent year.
# your code goes here