Week 8 HW Using DSLabs

Author

Ashley Ramirez

Load all the required packages/libraries

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dslabs)
library(highcharter)
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

Display available datasets in the dslabs package

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-calificaciones.R"               
 [5] "make-death_prob.R"                   
 [6] "make-divorce_margarine.R"            
 [7] "make-gapminder-rdas.R"               
 [8] "make-greenhouse_gases.R"             
 [9] "make-historic_co2.R"                 
[10] "make-mice_weights.R"                 
[11] "make-mnist_127.R"                    
[12] "make-mnist_27.R"                     
[13] "make-movielens.R"                    
[14] "make-murders-rda.R"                  
[15] "make-na_example-rda.R"               
[16] "make-nyc_regents_scores.R"           
[17] "make-olive.R"                        
[18] "make-outlier_example.R"              
[19] "make-polls_2008.R"                   
[20] "make-polls_us_election_2016.R"       
[21] "make-pr_death_counts.R"              
[22] "make-reported_heights-rda.R"         
[23] "make-research_funding_rates.R"       
[24] "make-stars.R"                        
[25] "make-temp_carbon.R"                  
[26] "make-tissue-gene-expression.R"       
[27] "make-trump_tweets.R"                 
[28] "make-weekly_us_contagious_diseases.R"
[29] "save-gapminder-example-csv.R"        

Select and Load Desired data

data("greenhouse_gases")

View Stucture of data

str(greenhouse_gases)
'data.frame':   300 obs. of  3 variables:
 $ year         : num  20 40 60 80 100 120 140 160 180 200 ...
 $ gas          : chr  "CO2" "CO2" "CO2" "CO2" ...
 $ concentration: num  278 278 277 277 278 ...

Prepare data by grouping it by gas type

data_grouped <- greenhouse_gases %>%
  group_by(gas) %>%
  arrange(year)

Create interactive plot

highchart() %>%
  hc_title(text = "Greenhouse Gas Concentrations Over Time") %>%
  hc_xAxis(title = list(text = "Year")) %>%
  hc_yAxis(title = list(text = "Concentration (ppm)")) %>%
  hc_add_series(
    data = filter(data_grouped, gas == "CO2"),
    type = "line",
    hcaes(x = year, y = concentration),
    name = "CO₂",
    color = "yellow"
  ) %>%
  hc_add_series(
    data = filter(data_grouped, gas == "CH4"),
    type = "line",
    hcaes(x = year, y = concentration),
    name = "CH₄",
    color = "darkcyan"
  ) %>%
  hc_add_series(
    data = filter(data_grouped, gas == "N2O"),
    type = "line",
    hcaes(x = year, y = concentration),
    name = "N₂O",
    color = "lightcoral"
  ) %>%
  hc_tooltip(pointFormat = "Concentration: {point.y} ppm") %>%
  hc_chart(zoomType = "x")

Project description

In this project I chose to work with the greenhouse_gases dataset from the package dslabs. This dataset provided information regarding the concentration of the main green house gases, C02, CH4, and N20, over the years. In order to create the plot i had to group each gas and arrange it from latest to recent years. Then I utilized highcharter to create an interactive line plot that shows each gas type, each with their respective color shown on the legend, and the concentration level according to the year shown in the x-axis, which can also be zoomed in.