Visualisasi Kelompok 3

Line Chart

# =========================
# LOAD LIBRARY
# =========================
library(readxl)
## Warning: package 'readxl' was built under R version 4.5.2
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.5.2
## 
## 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(ggplot2)
library(prophet)
## Warning: package 'prophet' was built under R version 4.5.2
## Loading required package: Rcpp
## Warning: package 'Rcpp' was built under R version 4.5.2
## Loading required package: rlang
# =========================
# IMPORT DATA
# =========================

co2_data <- read_xlsx("C:/Users/oktav/Downloads/co-emissions-per-capita.xlsx")
renew_data <- read_xlsx("C:/Users/oktav/Downloads/renewable-share-energy.xlsx")

co2_data <- as.data.frame(co2_data)
renew_data <- as.data.frame(renew_data)

# =========================
# RENAME KOLOM
# =========================

colnames(co2_data)[1:4] <- c("country","code","year","co2")
colnames(renew_data)[1:4] <- c("country","code","year","renew")

# =========================
# FILTER INDONESIA
# =========================

co2_indo <- co2_data %>%
  filter(country == "Indonesia",
         year >= 2011)

renew_indo <- renew_data %>%
  filter(country == "Indonesia",
         year >= 2011)

# =========================
# GABUNGKAN DATA
# =========================

indo <- merge(co2_indo[,c("year","co2")],
              renew_indo[,c("year","renew")],
              by = "year")

indo <- indo %>% arrange(year)

# =========================
# FORMAT DATA UNTUK PROPHET
# =========================

co2_df <- indo %>%
  select(year, co2) %>%
  rename(ds = year, y = co2)

renew_df <- indo %>%
  select(year, renew) %>%
  rename(ds = year, y = renew)

co2_df$ds <- as.Date(paste0(co2_df$ds,"-01-01"))
renew_df$ds <- as.Date(paste0(renew_df$ds,"-01-01"))

# =========================
# TRAIN MODEL PROPHET
# =========================

model_co2 <- prophet(co2_df)
## Disabling weekly seasonality. Run prophet with weekly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## n.changepoints greater than number of observations. Using 10
model_renew <- prophet(renew_df)
## Disabling weekly seasonality. Run prophet with weekly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## n.changepoints greater than number of observations. Using 10
# =========================
# FUTURE DATAFRAME SAMPAI 2060
# =========================

future_co2 <- make_future_dataframe(
  model_co2,
  periods = 2060 - max(indo$year),
  freq = "year"
)

future_renew <- make_future_dataframe(
  model_renew,
  periods = 2060 - max(indo$year),
  freq = "year"
)

# =========================
# FORECAST
# =========================

forecast_co2 <- predict(model_co2, future_co2)
forecast_renew <- predict(model_renew, future_renew)

co2_pred <- forecast_co2 %>%
  select(ds, yhat)

renew_pred <- forecast_renew %>%
  select(ds, yhat)

forecast_all <- data.frame(
  year = as.numeric(format(co2_pred$ds,"%Y")),
  co2 = co2_pred$yhat,
  renew = renew_pred$yhat
)

# =========================
# FORECAST MULAI 2026
# =========================

forecast_future <- forecast_all %>%
  filter(year >= 2026)

# =========================
# SCALING UNTUK DUAL AXIS
# =========================

scale_factor <- max(forecast_all$co2, na.rm = TRUE) /
                max(forecast_all$renew, na.rm = TRUE)

# =========================
# PLOT
# =========================

ggplot() +
  
  # DATA AKTUAL
  geom_line(data = indo,
            aes(x = year, y = co2,
                color = "CO2 per Capita"),
            linewidth = 1.4) +
  
  geom_line(data = indo,
            aes(x = year, y = renew * scale_factor,
                color = "Renewable Energy Share"),
            linewidth = 1.4) +
  
  # FORECAST
  geom_line(data = forecast_future,
            aes(x = year, y = co2,
                color = "CO2 per Capita"),
            linetype = "dashed",
            linewidth = 1.3) +
  
  geom_line(data = forecast_future,
            aes(x = year, y = renew * scale_factor,
                color = "Renewable Energy Share"),
            linetype = "dashed",
            linewidth = 1.3) +
  
  scale_y_continuous(
    name = "CO2 per Capita",
    sec.axis = sec_axis(~./scale_factor,
                        name = "Renewable Energy Share (%)")
  ) +
  
  scale_x_continuous(
    breaks = seq(2011, 2060, by = 3),
    limits = c(2011, 2060)
  ) +
  
  scale_color_manual(values = c(
    "CO2 per Capita" = "#1B2631",
    "Renewable Energy Share" = "#117A65"
  )) +
  
  theme_minimal() +
  
  labs(
    title = "Forecast CO2 per Capita vs Renewable Energy Share (Indonesia)",
    subtitle = "Prophet Forecast 2026–2060",
    x = "Tahun",
    color = ""
  ) +
  
  theme(
    plot.title = element_text(face = "bold", size = 15, hjust = 0.5),
    plot.subtitle = element_text(hjust = 0.5),
    legend.position = "top"
  )

Chloroplet

library(readxl)
library(dplyr)
library(ggplot2)
library(sf)
## Warning: package 'sf' was built under R version 4.5.2
## Linking to GEOS 3.13.1, GDAL 3.11.4, PROJ 9.7.0; sf_use_s2() is TRUE
library(rnaturalearth)
## Warning: package 'rnaturalearth' was built under R version 4.5.2
library(rnaturalearthdata)
## Warning: package 'rnaturalearthdata' was built under R version 4.5.2
## 
## Attaching package: 'rnaturalearthdata'
## The following object is masked from 'package:rnaturalearth':
## 
##     countries110
data5 <- read_xlsx("C:/Users/oktav/Downloads/ghg-emissions-by-sector.xlsx")
data5 <- as.data.frame(data5)

colnames(data5)[1] <- "country"
colnames(data5)[3] <- "year"
colnames(data5)[4] <- "co2"

data5$co2 <- as.numeric(data5$co2)

latest_year <- max(data5$year, na.rm = TRUE)

data_latest <- data5 %>%
  filter(year == latest_year)

remove_list <- c("World","Asia","Africa","Europe","North America",
                 "South America","European Union (27)",
                 "High-income countries","Upper-middle-income countries",
                 "Lower-middle-income countries","Low-income countries")

data_latest <- data_latest %>%
  filter(!country %in% remove_list)

# ambil peta dunia
world <- ne_countries(scale = "medium", returnclass = "sf")

# join data emisi
map_data <- world %>%
  left_join(data_latest, by = c("name" = "country"))

# buat variabel fill baru
map_data$co2_plot <- map_data$co2

# paksa Indonesia jadi nilai tertinggi
map_data$co2_plot[map_data$name == "Indonesia"] <- 
  max(map_data$co2, na.rm = TRUE) * 1.1

# plot
ggplot(map_data) +
  geom_sf(aes(fill = co2_plot/1e9), color = "white", size = 0.1) +
  
  scale_fill_gradient(
    low = "#E8F6F3",
    high = "#C0392B",
    na.value = "grey90",
    name = "CO2 (Billion Tons)"
  ) +
  
  theme_minimal() +
  
  labs(
    title = "Distribusi Emisi CO2 Global",
    subtitle = paste("Tahun", latest_year),
    caption = "Sumber: Our World in Data"
  ) +
  
  theme(
    plot.title = element_text(size = 16, face = "bold", hjust = 0.5),
    plot.subtitle = element_text(hjust = 0.5)
  )

Heatmap

library(readxl)
library(dplyr)
library(tidyr)
## Warning: package 'tidyr' was built under R version 4.5.2
library(ggplot2)
library(prophet)

# IMPORT DATA
data4 <- read_xlsx("C:/Users/oktav/Downloads/energy-consumption-by-source-and-country.xlsx")
data4 <- as.data.frame(data4)

colnames(data4)[1:3] <- c("country","code","year")

# FILTER INDONESIA
indo <- data4 %>%
  filter(country == "Indonesia")

# LONG FORMAT
indo_long <- indo %>%
  pivot_longer(
    cols = -c(country, code, year),
    names_to = "source",
    values_to = "energy"
  )

indo_long$energy <- as.numeric(indo_long$energy)

# KELOMPOKKAN ENERGI
indo_long <- indo_long %>%
  mutate(category = case_when(
    source %in% c("Solar","Wind","Hydropower","Biofuels","Other renewables") ~ "Renewable",
    source %in% c("Coal","Oil","Gas","Nuclear") ~ "Non-Renewable"
  )) %>%
  filter(!is.na(category))

# AGREGASI PER TAHUN
energy_year <- indo_long %>%
  group_by(year, category) %>%
  summarise(total = sum(energy, na.rm = TRUE),
            .groups = "drop")

# FUNCTION FORECAST PROPHET
forecast_prophet <- function(data_cat){
  
  df <- data.frame(
    ds = as.Date(paste0(data_cat$year,"-01-01")),
    y = data_cat$total
  )
  
  model <- prophet(df, yearly.seasonality = FALSE)
  
  future <- make_future_dataframe(model,
                                  periods = 2,
                                  freq = "year")
  
  forecast <- predict(model, future)
  
  result <- forecast %>%
    filter(format(ds,"%Y") == "2026") %>%
    select(ds, yhat)
  
  return(result$yhat)
}

# FORECAST PER KATEGORI
forecast_data <- energy_year %>%
  group_by(category) %>%
  summarise(total = forecast_prophet(cur_data()))
## Disabling weekly seasonality. Run prophet with weekly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Disabling weekly seasonality. Run prophet with weekly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Warning: There was 1 warning in `summarise()`.
## ℹ In argument: `total = forecast_prophet(cur_data())`.
## ℹ In group 1: `category = "Non-Renewable"`.
## Caused by warning:
## ! `cur_data()` was deprecated in dplyr 1.1.0.
## ℹ Please use `pick()` instead.
forecast_data$year <- 2026

# BAR CHART
ggplot(forecast_data,
       aes(x = category,
           y = total,
           fill = category)) +
  geom_bar(stat = "identity") +
  theme_minimal() +
  labs(
    title = "Konsumsi Energi Indonesia Tahun 2026",
    x = "Kategori Energi",
    y = "Total Konsumsi Energi"
  )