# Load the required packages
pacman::p_load(pacman, tidyverse, ggplot2, gridExtra)

# Load the data 
year <- c(1750, 1800, 1850, 1900, 1950, 2000, 2022, 2023)
emissions <- c(0.03, 0.07, 0.28, 1.71, 6.73, 20.64, 37.15, 40.9)

# Create a data frame
data <- data.frame(year, emissions)

# Fit a linear regression model with limited data
model <- lm(emissions ~ year + I(year^2), data = data)

# Make predictions for the 2030s
predictions <- predict(model, newdata = data.frame(year = 2030:2039))

# Create the first plot
plot1 <- ggplot(data, aes(x = year, y = emissions)) +
  geom_point(color = "#000000") +
  geom_line(color = "#000000") +
  geom_point(data = data.frame(year = 2030:2039, emissions = predictions), aes(x = year, y = emissions), color = "#FF0000") +
  labs(title = "1) Global CO₂ Emissions: Linear Regression Model Prediction 2030-2039 - Limited Data", x = "Year", y = "Emissions GtCO₂")

# Load data from CSV file
data <- read.csv("GlobalCO2Emissions.csv")

# Ensure variables are named correctly
names(data)[names(data) == "Year"] <- "year"
names(data)[names(data) == "Emissions"] <- "emissions"

# Fit a linear regression model with full data
model <- lm(emissions ~ year + I(year^2), data = data)

# Make predictions for 2030-2039
future_years <- 2030:2039
predictions <- predict(model, newdata = data.frame(year = future_years))

# Create the second plot
plot2 <- ggplot(data, aes(x = year, y = emissions)) +
  geom_point(color = "#000000") +
  geom_line(color = "#000000") +
  
  geom_point(data = data.frame(year = future_years, emissions = predictions), aes(x = year, y = emissions), color = "#FF0000") +
  labs(title = "2) Global CO₂ Emissions: Linear Regression Model Prediction 2030-2039 - Full Data", x = "Year", y = "Emissions GtCO₂")

# Load data
data <- data.frame(year = c(1750, 1800, 1850, 1900, 1950, 2000, 2022, 2023),
                   emissions = c(0.03, 0.07, 0.28, 1.71, 6.73, 20.64, 37.15, 40.9))

# Fit exponential model using log-transformation with limited data
model <- lm(log(emissions) ~ year, data = data)

# Predict for future years
future_years <- 2030:2039
predictions <- exp(predict(model, newdata = data.frame(year = future_years)))

# Create the third plot
plot3 <- ggplot(data, aes(x = year, y = emissions)) +
  geom_point() +
  geom_line() +
  geom_line(data = data.frame(year = future_years, emissions = predictions), color = "#FF0000") +
  labs(title = "3) Global CO₂ Emissions: Exponential Model Prediction 2030-2039 - Limited Data", x = "Year", y = "Emissions GtCO₂")

# Load data from CSV file
data <- read.csv("GlobalCO2Emissions.csv")

# Ensure variables are named correctly
names(data)[names(data) == "Year"] <- "year"
names(data)[names(data) == "Emissions"] <- "emissions"

# Fit exponential model using log-transformation with full data
model <- lm(log(emissions) ~ year, data = data)

# Predict for future years
future_years <- 2030:2039
predictions <- exp(predict(model, newdata = data.frame(year = future_years)))

# Create the fourth plot
plot4 <- ggplot(data, aes(x = year, y = emissions)) +
  geom_point() +
  geom_line() +
  geom_line(data = data.frame(year = future_years, emissions = predictions), color = "#FF0000") +
  labs(title = "4) Global CO₂ Emissions: Exponential Model Prediction 2030-2039 - Full Data", x = "Year", y = "Emissions GtCO₂")

# Combine the plots using grid.arrange
gridExtra::grid.arrange(plot1, plot2, plot3, plot4, nrow = 2, ncol = 2)