Persiapan Data

library(sf)
## Linking to GEOS 3.11.2, GDAL 3.6.2, PROJ 9.2.0; sf_use_s2() is TRUE
library(ggplot2)
library(viridis)
## Loading required package: viridisLite
library(treemapify)
library(sunburstR)
library(reshape2)
library(dplyr)
## 
## 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
# Baca file GeoJSON
klaten <- st_read("D:/Statistika Lingkungan Sem 5/33.10_Klaten.geojson")
## Reading layer `33.10_Klaten' from data source 
##   `D:\Statistika Lingkungan Sem 5\33.10_Klaten.geojson' using driver `GeoJSON'
## Simple feature collection with 1 feature and 3 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XYZ
## Bounding box:  xmin: 110.4452 ymin: -7.808604 xmax: 110.7996 ymax: -7.538835
## z_range:       zmin: 0 zmax: 0
## Geodetic CRS:  WGS 84
# Tambahkan data dummy populasi
set.seed(123)
klaten$Populasi <- sample(20000:60000, size = nrow(klaten), replace = TRUE)
klaten$AngkaKelahiran <- sample(10:40, size = nrow(klaten), replace = TRUE)

1. Choropleth Map

ggplot(data = klaten) +
  geom_sf(aes(fill = Populasi), color = "white") +
  scale_fill_viridis(option = "C", direction = -1) +
  labs(title = "Choropleth Populasi per Kecamatan - Klaten",
       fill = "Populasi") +
  theme_minimal()

# Load library
library(sf)
library(ggplot2)
library(dplyr)
# 1. Baca file GeoJSON Klaten (per kecamatan)
klaten <- st_read("D:/Statistika Lingkungan Sem 5/33.10_kecamatan.geojson")
## Reading layer `33.10_kecamatan' from data source 
##   `D:\Statistika Lingkungan Sem 5\33.10_kecamatan.geojson' using driver `GeoJSON'
## Simple feature collection with 26 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XYZ
## Bounding box:  xmin: 110.4452 ymin: -7.808604 xmax: 110.7996 ymax: -7.538835
## z_range:       zmin: 0 zmax: 0
## Geodetic CRS:  WGS 84
# 2. Cek nama kolom yang tersedia
names(klaten)
## [1] "kd_propinsi"  "kd_dati2"     "kd_kecamatan" "nm_kecamatan" "geometry"
# 3. Lihat data tabular tanpa geometry
head(st_set_geometry(klaten, NULL))
##   kd_propinsi kd_dati2 kd_kecamatan nm_kecamatan
## 1          33       10          001    Prambanan
## 2          33       10          002   Gantiwarno
## 3          33       10          003         Wedi
## 4          33       10          004        Bayat
## 5          33       10          005        Cawas
## 6          33       10          006       Trucuk
# Tambahkan data dummy Populasi kalau belum ada
set.seed(123)
klaten$Populasi <- sample(20000:60000, size = nrow(klaten), replace = TRUE)
klaten$AngkaKelahiran <- sample(10:40, size = nrow(klaten), replace = TRUE)
# 4. Ambil beberapa kecamatan untuk contoh donut chart
df_donut <- klaten %>%
  st_set_geometry(NULL) %>%
  select(NamaKec = nm_kecamatan, Populasi) %>%   
  head(6)   

2. Donut chart

ggplot(df_donut, aes(x = 2, y = Populasi, fill = NamaKec)) +
  geom_bar(stat = "identity", width = 1, color = "white") +
  coord_polar(theta = "y") +
  xlim(0.5, 2.5) +
  theme_void() +
  labs(title = "Donut Chart Populasi per Kecamatan (contoh)")

# 3. Bar Chart

df_bar <- klaten %>%
  st_set_geometry(NULL) %>%
  select(Kecamatan = nm_kecamatan, Populasi)

ggplot(df_bar, aes(x = reorder(Kecamatan, -Populasi), y = Populasi, fill = Kecamatan)) +
  geom_bar(stat = "identity") +
  coord_flip() +
  theme_minimal() +
  labs(title = "Bar Chart Populasi per Kecamatan")

4. Radial Bar Chart

ggplot(df_bar, aes(x = Kecamatan, y = Populasi, fill = Kecamatan)) +
  geom_bar(stat = "identity") +
  coord_polar(start = 0) +
  theme_minimal() +
  labs(title = "Radial Bar Chart Populasi per Kecamatan")

Buat dataframe tabular tanpa geometry

df_vars <- klaten %>%
  st_set_geometry(NULL) %>%
  select(Kecamatan = nm_kecamatan, Populasi, AngkaKelahiran)

5. Heatmap (Populasi & Angka Kelahiran)

df_long <- melt(df_vars, id.vars = "Kecamatan")

ggplot(df_long, aes(x = variable, y = Kecamatan, fill = value)) +
  geom_tile(color = "white") +
  scale_fill_viridis_c(option = "D") +
  theme_minimal() +
  labs(title = "Heatmap Variabel per Kecamatan",
       x = "Variabel", y = "Kecamatan", fill = "Nilai")

6. Treemap

ggplot(df_vars, aes(area = Populasi, fill = Populasi, label = Kecamatan)) +
  geom_treemap() +
  geom_treemap_text(colour = "white", place = "centre") +
  scale_fill_viridis_c(option = "C") +
  labs(title = "Treemap Populasi per Kecamatan")