` # Examining birth rates in the world’s four largest ecomonies between 1990 and 2014

Set your working directory to access your files

setwd("C:/Documents - Copy/PERSONAL/Data 110_MC_Class")

Load required packages

# load required packages
library(readr)
library(ggplot2)
library(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:readr':
## 
##     col_factor
library (fansi)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v tibble  3.1.2     v dplyr   1.0.6
## v tidyr   1.1.3     v stringr 1.4.0
## v purrr   0.3.4     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x scales::col_factor() masks readr::col_factor()
## x purrr::discard()     masks scales::discard()
## x dplyr::filter()      masks stats::filter()
## x dplyr::lag()         masks stats::lag()
library(dbplyr)
## 
## Attaching package: 'dbplyr'
## The following objects are masked from 'package:dplyr':
## 
##     ident, sql

Install and load required packages

library(highcharter)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(RColorBrewer)

Load and process nations data

Download “nations” data from the class’s datasets and save it in the default directory.

Load the nations data from the default directory

nations <- read_csv("nations.csv")
## 
## -- Column specification --------------------------------------------------------
## cols(
##   iso2c = col_character(),
##   iso3c = col_character(),
##   country = col_character(),
##   year = col_double(),
##   gdp_percap = col_double(),
##   population = col_double(),
##   birth_rate = col_double(),
##   neonat_mortal_rate = col_double(),
##   region = col_character(),
##   income = col_character()
## )
  str(nations)
## spec_tbl_df [5,275 x 10] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ iso2c             : chr [1:5275] "AD" "AD" "AD" "AD" ...
##  $ iso3c             : chr [1:5275] "AND" "AND" "AND" "AND" ...
##  $ country           : chr [1:5275] "Andorra" "Andorra" "Andorra" "Andorra" ...
##  $ year              : num [1:5275] 1996 1994 2003 1990 2009 ...
##  $ gdp_percap        : num [1:5275] NA NA NA NA NA NA NA NA NA NA ...
##  $ population        : num [1:5275] 64291 62707 74783 54511 85474 ...
##  $ birth_rate        : num [1:5275] 10.9 10.9 10.3 11.9 9.9 NA 10.9 9.8 11.8 11.2 ...
##  $ neonat_mortal_rate: num [1:5275] 2.8 3.2 2 4.3 1.7 1.6 2 1.7 2.1 2.1 ...
##  $ region            : chr [1:5275] "Europe & Central Asia" "Europe & Central Asia" "Europe & Central Asia" "Europe & Central Asia" ...
##  $ income            : chr [1:5275] "High income" "High income" "High income" "High income" ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   iso2c = col_character(),
##   ..   iso3c = col_character(),
##   ..   country = col_character(),
##   ..   year = col_double(),
##   ..   gdp_percap = col_double(),
##   ..   population = col_double(),
##   ..   birth_rate = col_double(),
##   ..   neonat_mortal_rate = col_double(),
##   ..   region = col_character(),
##   ..   income = col_character()
##   .. )

Prepare the data by filtering for the four largest economies: China, Germany, Japan and USA.

The dataset has 5,275 entries.

# prepare data
big4 <- nations %>%
  filter(iso3c == "CHN" | iso3c == "DEU" | iso3c == "JPN" | iso3c == "USA") %>%
  arrange(year)

Check the data to comfirm filtering

view(big4)

Yes big4 has only 100 entries and only China, Germany, Japan and USA are the only countries appearing in the country column.

Plot big-4 against birth_rate.

# basic symbol-and-line chart, default settings
highchart() %>%
  hc_add_series(data = big4,
                   type = "line", hcaes(x = year,
                   y = birth_rate, 
                   group = country))

#Use a ColorBrewer palette

Use 4 colors from “set1”

# define color palette
cols <- brewer.pal(4, "Set1")

highchart() %>%
  hc_add_series(data = big4,
                   type = "line", hcaes(x = year,
                   y = birth_rate, 
                   group = country)) %>%
  hc_colors(cols)

Add axis labels

highchart() %>%
  hc_add_series(data = big4,
                   type = "line",
                   hcaes(x = year,
                   y = birth_rate, 
                   group = country)) %>%
  hc_colors(cols) %>%
  hc_xAxis(title = list(text="Year")) %>%
  hc_yAxis(title = list(text="birth_rate"))

Add a title

highchart() %>%
  hc_add_series(data = big4,
                   type = "line",
                   hcaes(x = year,
                   y = birth_rate, 
                   group = country)) %>%
  hc_colors(cols) %>%
  hc_xAxis(title = list(text="Year")) %>%
  hc_yAxis(title = list(text="birth_rate"))%>%
  hc_title(
    text = "Birth rates in the four largest economies of the world",
    margin = 20,
    align = "left",
    style = list(color = "#22A884", useHTML = TRUE)
    )

Conclusion

The data shows that the birth rates of Japan and Germany have been low since 1990 and have continued to drop. However, the birth rates of China and the USA have been higher than the rates in Germany and Japan during this period. Notably the rate in China was much higher in 1990 but it started to fall drastically, probably because of the one-child policy and it fell below the USA birth rate in the year 2000. Thereafter, the Chinese birth rate started to flatten and in 2014 it was similar to the USA birth rate which had been falling steadily. The flattened curve of the Chinese birth rate was probably due to the more recent relaxation of child birth policy in the country.