library("dslabs") # installs packages("dslabs") which we will use for the homework
## Warning: package 'dslabs' was built under R version 4.0.4
data(package="dslabs")
list.files(system.file("script", package = "dslabs"))
##  [1] "make-admissions.R"                   
##  [2] "make-brca.R"                         
##  [3] "make-brexit_polls.R"                 
##  [4] "make-death_prob.R"                   
##  [5] "make-divorce_margarine.R"            
##  [6] "make-gapminder-rdas.R"               
##  [7] "make-greenhouse_gases.R"             
##  [8] "make-historic_co2.R"                 
##  [9] "make-mnist_27.R"                     
## [10] "make-movielens.R"                    
## [11] "make-murders-rda.R"                  
## [12] "make-na_example-rda.R"               
## [13] "make-nyc_regents_scores.R"           
## [14] "make-olive.R"                        
## [15] "make-outlier_example.R"              
## [16] "make-polls_2008.R"                   
## [17] "make-polls_us_election_2016.R"       
## [18] "make-reported_heights-rda.R"         
## [19] "make-research_funding_rates.R"       
## [20] "make-stars.R"                        
## [21] "make-temp_carbon.R"                  
## [22] "make-tissue-gene-expression.R"       
## [23] "make-trump_tweets.R"                 
## [24] "make-weekly_us_contagious_diseases.R"
## [25] "save-gapminder-example-csv.R"
#installas packages for Highcharter
library(readr) 
library(ggplot2)
library(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:readr':
## 
##     col_factor
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(highcharter)
## Warning: package 'highcharter' was built under R version 4.0.4
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## 
## Attaching package: 'highcharter'
## The following object is masked from 'package:dslabs':
## 
##     stars
library(RColorBrewer)
library(dplyr) #installs dplyr package
nations <- read_csv("nations.csv") %>% # uploads data
  mutate(gdp_tn = gdp_percap*population/1000000000000) #adds a new column gdp in trillion
## 
## -- 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()
## )
big4 <- nations %>% 
  filter(iso3c == "CHN" | iso3c == "DEU" | iso3c == "JPN" | iso3c == "USA") %>% #o the nations dataset, the countries: China, Germany, Japan and United States are pulled
  arrange(year) # arranges per year
cols <- brewer.pal(4, "Set2") #sets the color palette for graph

big4_chart <- highchart() %>% 
  hc_add_series(data = big4,
                   type = "line",
                   hcaes(x = year,
                   y = gdp_tn, 
                   group = country)) %>% # this creates a line chart based on year and gdp
  hc_colors(cols) %>% #this is puts the palette that we selected previously
  hc_xAxis(title = list(text="Year")) %>% # adds title to x axis
  hc_yAxis(title = list(text="GDP ($ trillion)")) %>% #adds title to y axis
  hc_plotOptions(series = list(marker = list(symbol = "circle"))) %>% # this select the marker type i.e, circle
  hc_legend(align = "right", 
            verticalAlign = "top") %>% # moves the legend to the top of the graph rather than preset  bottom
  hc_tooltip(shared = TRUE,
             borderColor = "black",
             pointFormat = "{point.country}: {point.gdp_tn:.2f}<br>") #edits tooltip for each year to include the four countries and  round  the gdp numbers to two decimal places 
big4_chart

I used the nation dataset that was included in the notes for this homework but added birth rate as an additional y axis so that birth rates and GDPs can be compared on the same x axis

highchart() %>%
   hc_yAxis_multiples(
    list(title = list(text = "gdp (trillions)")),
    list(title = list(text = "birth_rate"), 
         opposite = TRUE) # This adds two y axis to the same x axis
  ) %>%
  
  hc_add_series(data = big4,
                   type = "line", 
                   hcaes(x = year,
                   y = gdp_tn, 
                   group = country), name = c("China GDP", "Germany GDP", "Japan GDP", "United State GDP"),
                   yAxis=0) %>% # this creates the line chart for x= year and y = gdp  per country
  
    hc_add_series(data = big4, 
                   type = "line",  
                   hcaes(x = year,
                   y = birth_rate, 
                   group = country),  name = c("China Birth", "Germany Birth", "Japan Birth", "United States Birth"),
                   yAxis=1) %>% # this creates the line chart for x= year and y = birth rate per country
  
  hc_colors(cols) %>% #adds the color per country
  hc_xAxis(title = list(text="Year")) %>% # adds title to x axis
  #hc_yAxis(title = list(text="GDP ($ trillion)")) %>% # adds title to y axis
  hc_plotOptions(series = list(marker = list(symbol = "circle"))) %>%  # this select the marker type i.e, circle 
  hc_legend(align = "right", 
            verticalAlign = "top") #moves the legend to the top of the graph rather than preset  bottom

The previous graph was very busy and confusing so I decided to do the same graph but instead, I removed the color palette and used one color for GDP and a another one for birth rate

highchart() %>%
   hc_yAxis_multiples(
    list(title = list(text = "gdp (trillions)")),
    list(title = list(text = "birth_rate"), 
         opposite = TRUE) # This adds two y axis to the same x axis.
  ) %>%
  
  hc_add_series(data = big4,
                   type = "line", color = "purple",
                   hcaes(x = year,
                   y = gdp_tn, 
                   group = country), name = c("China GDP", "Germany GDP", "Japan GDP", "United State GDP"),
                   yAxis=0) %>% # this creates the line chart for x= year and y = gdp  per country
  
    hc_add_series(data = big4, 
                   type = "line",   color = "pink",
                   hcaes(x = year,
                   y = birth_rate, 
                   group = country),  name = c("China Birth", "Germany Birth", "Japan Birth", "United States Birth"),
                   yAxis=1) %>% # this creates the line chart for x= year and y = birth rate per country
  
  hc_xAxis(title = list(text="Year")) %>% # adds title to x axis
  #hc_yAxis(title = list(text="GDP ($ trillion)")) %>% # adds title to y axis
  hc_plotOptions(series = list(marker = list(symbol = "circle"))) %>%  # this select the marker type i.e, circle 
  hc_legend(align = "right", 
            verticalAlign = "top") #moves the legend to the top of the graph rather than preset  bottom

```