R (Team 2020) adalah bahasa interpreter untuk analisis data yang termasuk aplikasi statistik open source. Bahasa R pertama kali dirancang oleh Ross Ihaka dan Robert Gentleman dan dirilis pada Agustus 1993. Bahasa R terpengaruh dari Bahasa S yang telah dirilis sejak tahun 1976. Bahasa S telah dirancang oleh John Chambers dan Rick Becker (Ihaka and Gentleman 1996). Bahasa R dapat dipasang pada komputer bersistem operasi Linux, MacOS, dan Windows. Untuk memasang R pada komputer pribadi, bahasa kompilasi seperti C dan C++ diperlukan, seperti GCC (GNU Compiler Collection).Saat ini, pengguna statistika di Indonesia sudah banyak menggunakan R untuk keperluan analisis data dan data berukuran besar (big data). Aplikasi statistik komersil, seperti SPSS, Minitab, SAS, atau SAP masih digunakan karena kemudahan fasilitas dan layanan. Ada empat alasan utama R dipilih oleh data scientist, yaitu:
Bahasa R mampu melakukan analisis data statistik dengan dukungan beberapa paket (packages). Untuk menuliskan algoritma R, Anda bisa menggunakan aplikasi teks editor seperti Notepad, Notepad++, Sublime, Atom, dan Visual Studio Code. Modul praktikum ini menggunakan Integrated Development Environment (IDE), RStudio, untuk menuliskan dan menjalankan kode-kode R.
Pada eksperimen kali ini, sumber data yang digunakan adalah Global Forest Watch berupa data deforestasi atau tree cover lost dari tahun 2001-2022. Data dapat di unduh pada tautan berikut : here is a link to the source.
library(lubridate)
library(dplyr)
library(tidyverse)
library(ggplot2)
library(readxl)
library(reshape2)
library(gridExtra)
Pada bagian ini, kita membaca dan memanipulasi data awal
read_excel().tc_loss_ha
menjadi hanya angka tahun dengan menggunakan rename_with()
dan str_remove.mutate() dan rowSums().#load datasets
tc_lost <- read_excel("C:/Users/A C E R/Downloads/IDN(5).xlsx", sheet = 4)
#Manipulate the datasets
tc_lost <- tc_lost %>%
rename_with(~ str_remove(., "tc_loss_ha_"), starts_with("tc_loss_ha_"))
#Calculate total losses by Thershold
total_losses <- tc_lost %>%
rowwise() %>%
mutate(total_loss_2001_2022 = rowSums(across(`2001`:`2022`)))
Data filtering adalah proses penyaringan data berdasarkan kriteria tertentu:
filter() dari paket dplyr
dapat digunakan untuk filtering data.# TOTAL INDONESIAN TREE COVER LOST (2001-2022)
indonesian_melted <- melt(total_losses, id.vars = c("threshold"), measure.vars = 8:29,
variable.name = "year", value.name = "total_loss")
# Convert 'year' column to numeric
indonesian_melted$year <- as.numeric(indonesian_melted$year)
Kode di atas adalah contoh implementasi pemrosesan data menggunakan bahasa pemrograman R. Tujuannya adalah untuk mengolah data tentang total kerugian tutupan pohon di Indonesia dari tahun 2001 hingga 2022.
melt() dari library(reshape2)
tertentu.as.numeric.# Manipulate datasets
long_data <- tc_lost %>%
pivot_longer(cols = c(`2001`:`2022`),
names_to = "year",
values_to = "loss")
# Total per province and threshold
total_loss_per_province <- long_data %>%
filter(year >= 2001 & year <= 2022) %>%
group_by(country, subnational1, threshold) %>%
summarise(total_loss = sum(loss))
# Menghitung total akumulasi kerugian untuk setiap provinsi dan threshold
total_loss_accumulated <- total_loss_per_province %>%
group_by(country, subnational1, threshold) %>%
summarise(total_loss = sum(total_loss) / 1000000)
Selanjutnya, data tersebut dikelompokkan berdasarkan negara, wilayah
subnasional (province), dan threshold (ambang batas) menggunakan fungsi
`group_by()`. Kemudian, total kerugian dihitung dan dijumlahkan untuk
setiap kelompok menggunakan summarise(), disimpan dalam
variabel total_loss_per_province.
Visualisasi data memiliki peran krusial dalam mengubah informasi yang kompleks menjadi wawasan yang dapat dipahami dengan mudah, memungkinkan identifikasi pola, anomali, dan hubungan dalam data, serta mendukung pengambilan keputusan yang informasional. Dengan membantu menyajikan cerita data yang kuat, visualisasi memfasilitasi komunikasi efektif, validasi hipotesis, dan penemuan wawasan baru, menjadikannya alat penting dalam analisis data.
# Create a time series plot
indonesian_plot <- ggplot(indonesian_melted, aes(x = year, y = total_loss/1000000, fill = factor(threshold))) +
geom_bar(stat = "identity", position = "dodge") +
labs(
title = "Indonesian Tree Cover Lost (2001-2022)",
x = "Year",
y = "Total Tree Cover Lost (MHa)",
fill = "Threshold"
) +
theme_minimal() +
theme(legend.position = "top")
indonesian_plot
# Membuat plot total loss per provinsi dari yang terbesar hingga terkecil (diagram horizontal)
per_province_plot <- ggplot(total_loss_accumulated, aes(x = total_loss, y = reorder(subnational1, total_loss), fill = factor(threshold))) +
geom_bar(stat = "identity", position = "dodge") +
labs(
title = "Total Loss per Province in Indonesia (2001-2022)",
x = "Total Loss (Mha)",
y = "Province",
fill = "Threshold"
) +
theme_minimal() +
theme(legend.position = "none") +
guides(fill = guide_legend(title = "Threshold"))
per_province_plot
In 2001, Indonesia had 93.8Mha of primary forest, extending over 50% of its land area. In 2022, it lost 230kha of primary forest, equivalent to 177Mt of CO₂ emissions. From 2001 to 2022, Indonesia lost 10.3Mha of humid primary forest, making up 35% of its total tree cover loss in the same time period. Total area of humid primary forest in Indonesia decreased by 11% in this time period.
# Menghitung total akumulasi kerugian untuk setiap threshold
total_loss_accumulated <- total_loss_per_province %>%
group_by(threshold) %>%
summarise(total_loss = sum(total_loss) / 1000000) # Mengubah satuan dari ha ke mha
# Membuat plot total akumulasi kerugian di seluruh Indonesia dari berbagai threshold
ggplot(total_loss_accumulated, aes(x = threshold, y = total_loss)) +
geom_ribbon(aes(ymin = 20, ymax = total_loss), fill = "green", alpha = 0.5) +
geom_line(aes(y = total_loss), color = "red", size = 1.5) + # Mengatur ketebalan garis
geom_point(aes(y = total_loss), color = "black", size = 1.5) + # Menambahkan titik pada garis
labs(
title = "Total Accumulated Losses in Indonesia by Threshold",
x = "Threshold",
y = "Total Accumulated Loss (Mha)"
) +
geom_text(aes(label = round(total_loss, 1)), hjust = -0.2, vjust = -0.2, size = 5) +
theme_minimal()
#DATA FILTERING
# Filter data for exmaple Riau Province
riau_data <- total_losses %>% filter(country == "Indonesia", subnational1 == "Riau") #subnational1 %in% c("Riau", "Aceh"))
# Menentukan skala warna gradient
color_gradient <- colorRampPalette(c("red", "yellow"))
# Membuat diagram batang dengan gradient warna
barplot(riau_data$total_loss_2001_2022/1000000,
names.arg = riau_data$threshold,
main = "Tree Cover Lost in Riau Province",
xlab = "Threshold",
ylim = c(0,4.5),
ylab = "Total Loss (Mha)",
col = color_gradient(length(riau_data$total_loss_2001_2022)))
# TIME SERIES TREE COVER LOST
# Melt the data into long format with year column
library(reshape2)
riau_melted <- melt(riau_data, id.vars = c("threshold"), measure.vars = 8:29,
variable.name = "year", value.name = "total_loss_2001_2022")
# Convert 'year' column to numeric
riau_melted$year <- as.numeric(riau_melted$year)
# Create a time series plot
library(gridExtra)
grid.arrange(ggplot(riau_melted, aes(x = year, y = total_loss_2001_2022, color = factor(threshold))) +
geom_line() +
labs(
title = "Line Plot (2001-2022)",
x = "Year",
y = "Total Loss (Ha)",
color = "Threshold"
) +
theme_minimal(),
# Create a grouped bar plot
ggplot(riau_melted, aes(x = year, y = total_loss_2001_2022, fill = factor(threshold))) +
geom_bar(stat = "identity", position = "dodge") +
labs(
title = "Bar Plot (2001-2022)",
x = "Year",
y = "Total Loss (Ha)",
fill = "Threshold"
) +
theme_minimal() +
theme(legend.position = "top"))