MIDTERM PROJECT

Economic Growth and Life Expectancy – Do Wealthier Countries Live Longer?

By choosing Life Expectancy and GDP per capita as variables, I try to find out if the increase in life expectancy is a key indicator to gauge the economic development of a country.

LOAD THE LIBRARIES AND THE DATA

gapminder: Data from Gapminder

An excerpt of the data available at Gapminder.org. For each of 142 countries, the package provides values for life expectancy, GDP per capita, and population, every five years, from 1952 to 2007.

# install.packages("dslabs")  # these are data science labs
library("dslabs")
## Warning: package 'dslabs' was built under R version 4.0.2
data(package="dslabs")
#(system.file("script", package = "dslabs"))
# The tidyverse is an opinionated collection of R packages designed for data science. All packages share an underlying design philosophy, grammar, and data structures.


#  The easiest way to get ggplot2 is to install the whole tidyverse:
# install.packages("tidyverse")
# install.packages("tidyverse")
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.2
## -- Attaching packages -------------------------------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.1     v purrr   0.3.4
## v tibble  3.0.1     v dplyr   1.0.0
## v tidyr   1.1.0     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## -- Conflicts ----------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(ggthemes)
## Warning: package 'ggthemes' was built under R version 4.0.2
#ggrepel: Automatically Position Non-Overlapping Text Labels with 'ggplot2'
#Provides text and label geoms for 'ggplot2' that help to avoid overlapping text labels. Labels repel away from each other and away from the data points.
library(ggrepel)
## Warning: package 'ggrepel' was built under R version 4.0.2
# Create interactive web graphics from 'ggplot2' graphs
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
# Loads specified data sets, or list the available data sets.
data(gapminder, package = "gapminder")

gg <- ggplot(gapminder, aes(gdpPercap, lifeExp, color = continent)) +
  geom_point(aes(size = pop, frame = year, ids = country)) +
  scale_x_log10() + xlab("GDP Per Capita") + ylab("Life Expectancy") +
  theme(
axis.title.x = element_text(color="blue", size=14, face="bold"),
axis.title.y = element_text(color="#993333", size=14, face="bold"))
## Warning: Ignoring unknown aesthetics: frame, ids
ggplotly(gg)

The Graph recreates the animation of the evolution in the relationship between GDP per capita and life expectancy evolved over time. As long as a frame variable is provided, an animation is produced with play/pause button(s) and a slider component for controlling the animation. These components can be removed or customized via the animation_button() and animation_slider() functions. Moreover, various animation options, like the amount of time between frames, the smooth transition duration, and the type of transition easing may be altered via the animation_opts() function. There’s a strong relationship between GDP and life expectancy. The graph show most of developing countries at the bottom compared to the developed.

USING HTML Widgets and Highcharter (WITH THE SAME DATASET)

HTML widgets work just like R plots except they produce interactive web visualizations. A line or two of R code is all it takes to produce a D3 graphic or Leaflet map. HTML widgets can be used at the R console as well as embedded in R Markdown reports and Shiny web applications. hchart is a generic function which takes an object and returns a highcharter object. There are functions whose behavior are similar to the functions of the ggplot2 package like:

hchart works like ggplot2’s qplot. hc_add_series works like ggplot2’s geom_S. hcaes works like ggplot2’s aes.

Load the libraries

The gapminder data set comes built in with dslabs.

# install.packages("dslabs")  # these are data science labs
library("dslabs")
library(tidyverse)
library(highcharter)
## Warning: package 'highcharter' was built under R version 4.0.2
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## Highcharts (www.highcharts.com) is a Highsoft software product which is
## not free for commercial and Governmental use
## 
## Attaching package: 'highcharter'
## The following object is masked from 'package:dslabs':
## 
##     stars
library(gapminder)
## Warning: package 'gapminder' was built under R version 4.0.2
## 
## Attaching package: 'gapminder'
## The following object is masked from 'package:dslabs':
## 
##     gapminder

Load the data

#library(dslab)
#glimpse(gapminder)

data(gapminder, package = "gapminder")

The function distinct() [dplyr package] can be used to keep only unique/distinct rows from a data frame. The option .kep_all is used to keep all variables in the data.

gp <- gapminder %>%
  arrange(desc(year)) %>%
  distinct(country, .keep_all = TRUE) 

gp2 <- gapminder %>%
  select(country, year, pop) %>% 
  nest(-country) #%>%
## Warning: All elements of `...` must be named.
## Did you want `data = c(year, pop)`?
gptot <- left_join(gp, gp2, by = "country")
x <- c("Population", "Year", "GDP PerCp", "Life Expectancy", "Continent")
y <- sprintf("{point.%s:.0f}", c("pop", "year", "gdpPercap", "lifeExp", 'continent'))

tltip <- tooltip_table(x, y)

# hchart() is a generic function to draw different charts on the fly. The resulting chart is a highchart object so you can keep modifying with the implmented API. If you are familiar with ggplot2, this function is similar to qplot() of it.


hchart(
  gptot,
  "point",
  hcaes(lifeExp, gdpPercap, name = country, size = pop, group = continent)
  ) %>%
  
#hc_yAxis(type = "logarithmic") %>%
hc_xAxis(title = list(text="Life Expectancy")) %>%
hc_yAxis(title = list(text="GDP Per Capita")) %>%
hc_legend(align = "center",
    verticalAlign = "bottom",
    layout = "horizontal"
    
  ) %>%  
hc_tooltip(
        useHTML = TRUE,
        headerFormat = "<b>{point.key}</b>",
        pointFormat = tltip )
## Warning: `parse_quosure()` is deprecated as of rlang 0.2.0.
## Please use `parse_quo()` instead.
## This warning is displayed once per session.
## Warning: `mutate_()` is deprecated as of dplyr 0.7.0.
## Please use `mutate()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## Warning: `group_by_()` is deprecated as of dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## Warning: `select_()` is deprecated as of dplyr 0.7.0.
## Please use `select()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## Warning: `as_data_frame()` is deprecated as of tibble 2.0.0.
## Please use `as_tibble()` instead.
## The signature and semantics have changed, see `?as_tibble`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## Warning: `rename_()` is deprecated as of dplyr 0.7.0.
## Please use `rename()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.