library(tidyverse)
library(dslabs)
library(highcharter)
data(temp_carbon)DS Labs
Load the library and dataset
Cleaning the dataset- removing missing values Grouping the data by decade
temp_carbon_cleaned <- temp_carbon |>
mutate(
decade = factor(floor(year / 10) * 10)
) |>
filter(!is.na(carbon_emissions) & !is.na(temp_anomaly))Plotting carbon emissions vs temperature anomaly in a static chart
chart_static <- temp_carbon_cleaned |>
ggplot(aes(x = carbon_emissions, y = temp_anomaly, color = decade)) +
geom_point(alpha = 0.8, size = 3.5) +
geom_smooth(method = "lm", se = FALSE, color = "blue", linewidth = .8, linetype = "solid") +
scale_color_viridis_d(option = "D") +
theme_minimal(base_size = 14) +
labs(
title = "Global Warming Trend: Emissions vs. Temperature Anomaly",
subtitle = "Data from 1850–2011",
x = "Annual Carbon Emissions in Metic Tons",
y = "Temperature Anomaly in Celsius",
color = "Decade"
) +
theme(
legend.position = "bottom",
plot.title = element_text(hjust = 0.5)
)
chart_staticUsing highcharter to create an Interactive chart
chart_interactive <- temp_carbon_cleaned |>
hchart(
type = "scatter",
hcaes(x = carbon_emissions, y = temp_anomaly, group = decade)
) |>
hc_title(text = "Interactive: Emissions vs. Temperature Anomaly by Decade")|>
hc_xAxis(title = list(text = "Annual Carbon Emissions in Metric Tons"))|>
hc_yAxis(title = list(text = "Temperature Anomaly in Celsius"))|>
hc_tooltip(pointFormat = paste(
"<b>Decade:</b> {point.group}<br>",
"<b>Emissions:</b> {point.x:,.0f} MT<br>",
"<b>Anomaly:</b> {point.y:.2f} °C"
))
chart_interactiveRunning a linear regresion model to see the relationship between emissions and temperature anomaly
lm_model <- lm(temp_anomaly ~ carbon_emissions, data = temp_carbon_cleaned)
summary(lm_model)
Call:
lm(formula = temp_anomaly ~ carbon_emissions, data = temp_carbon_cleaned)
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
Checking how well the model fits
par(mfrow = c(2, 2))
plot(lm_model)par(mfrow = c(1, 1))In this project I used the ‘temp_carbon’ dataset from the ‘dslabs’ package to study the connection between global carbon emissions and temperature changes/anomalies from 1850 to 2011. I cleaned the data, grouped it by decades, and then I made both a static and interactive plot to show how emissions and temperature have gone up together over time. The clear upward trend indicates that higher carbon emissions are heavily likely linked to higher global temperatures, especially in recent decades. My regression model supported this relationship with a positive slope. The diagnostic plots suggested the model fits well. I saw that in the 1940’s the anomalies rose quicker than they had normally, and I thought that could be linked to the events of WW2, where they were mass producing weapons and new tech. In conclsuion, this visualization highlights the rising impact of human activity on climate change and how temperature rise has accelerated with recent activity(indsutrial development likely).