jpark_assignment7

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Running Code

library(dslabs)
library(highcharter)
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

다음의 패키지를 부착합니다: 'highcharter'
The following object is masked from 'package:dslabs':

    stars
library(RColorBrewer)
library(dplyr)
Warning: 패키지 'dplyr'는 R 버전 4.5.3에서 작성되었습니다

다음의 패키지를 부착합니다: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(tidyverse)
Warning: 패키지 'tidyverse'는 R 버전 4.5.3에서 작성되었습니다
Warning: 패키지 'ggplot2'는 R 버전 4.5.3에서 작성되었습니다
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats   1.0.1     ✔ readr     2.1.6
✔ ggplot2   4.0.2     ✔ stringr   1.6.0
✔ lubridate 1.9.5     ✔ tibble    3.3.1
✔ purrr     1.2.1     ✔ tidyr     1.3.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
carbon_processed <- temp_carbon |>
  filter(!is.na(carbon_emissions) & !is.na(temp_anomaly)) |>
  mutate(century = case_when(
    year < 1900 ~ "19th Century",
    year < 2000 ~ "20th Century",
    TRUE ~ "21st Century"))

cols <- brewer.pal(3, "Set1")

highchart() |>
  hc_add_series(data = carbon_processed,
                type = "scatter", 
                hcaes(x = carbon_emissions, 
                      y = temp_anomaly, 
                      group = century, 
                      name = year)) |> 
  
  hc_colors(cols) |>
  hc_tooltip(pointFormat = "Year: {point.name} 
             <br> Emissions: {point.x} 
             <br> Anomaly: {point.y}") |>
  hc_title(text = "Impact of Carbon Emissions on Global Temperature") |>
  hc_subtitle(text = "Observation of temperature anomalies across different centuries") |>

  hc_xAxis(title = list(text = "Carbon Emissions (Million Metric Tons)")) |>
  hc_yAxis(title = list(text = "Temperature Anomaly (Celsius)")) |>
  hc_chart(style = list(fontFamily = "Arial", fontWeight = "bold")) |>
  hc_credits(enabled = TRUE, text = "Data Source: DS Labs (temp_carbon)") |>
  hc_legend(align = "right", 
            verticalAlign = "middle",
            layout = "vertical")
carbon_model <- lm(temp_anomaly ~ carbon_emissions, data = carbon_processed)
summary(carbon_model)

Call:
lm(formula = temp_anomaly ~ carbon_emissions, data = carbon_processed)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.29704 -0.07938 -0.00903  0.09615  0.40084 

Coefficients:
                   Estimate Std. Error t value Pr(>|t|)    
(Intercept)      -2.591e-01  1.690e-02  -15.33   <2e-16 ***
carbon_emissions  9.994e-05  4.202e-06   23.78   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.1338 on 133 degrees of freedom
Multiple R-squared:  0.8096,    Adjusted R-squared:  0.8082 
F-statistic: 565.7 on 1 and 133 DF,  p-value: < 2.2e-16
plot(carbon_model)

I created a scatter plot based on the temp_carbon dataset of the DS Labs package in order to see the correlation between carbon emissions and global warming trends. I set carbon emissions in units of million metric tons on the X-axis and temperature anomaly in units of Celsius on the Y-axis. To satisfy the requirement for a third variable, I categorized the data into three distinct centuries (the 19th, 20th, and 21st), using mutate and case_when functions. Also, to improve the visibility of the chart, I applied a non-default theme by customizing the font family and weight, and made it distinguishable from the standard ggplot style. Finally, to clearly distinguish the century visually, I displayed them in different colors using the Set1 palette of RColorBrewer.

What I could confirm through this visualization is that during the 19th and early 20th centuries, carbon emissions and temperature anomalies remained relatively stable, but starting from the late 20th century into the 21st century, a sharp and dramatic increase appeared in both variables. By choosing a scatter plot instead of a simple line graph, I was able to simultaneously check the yearly values and the overall trend of how the temperature anomaly changes as carbon emissions accelerate.

Additionally, I conducted a linear regression analysis (lm) to verify the statistical influence of carbon emissions on temperature anomaly, and confirmed that the causal relationship between the two variables is statistically very significant as the p-value for carbon emissions was significantly lower than the significance level of 0.05.