library (knitr)

title: “Tugas Bintel R Markdown” author: “Aura Najwa Chaerunnisa” date: “2025-05-25” output: html_document —

Business Intelligence

Nama : Aura Najwa Chaerunnisa

NRP : 5006221067

Tujuan dan Deskripsi Data

Pada studi kasus ini data yang dipakai adalah dataset Indeks Kualitas Udara dan Kasus Penyakit Pernapasan per Kota dengan memuat 5 variabel, diantaranya variable kota, provinsi, AQI, Kasus, dan Penduduk. Untuk menginterpretasikan dan menganalisis data menjadi lebih mudah, maka dilakukan pembuatan visualisasi. Pembuatan visualisasi dapat menggunakan Lattice, ggplot2, dan Graphics Engine.

Berikut ini adalah visualisasi data pada indeks kulitas udara dan kasus penyakit pernapasan yang melibatkan variable Kasus dan variable AQI.

Scatterplot Kasus (Y) vs AQI (X) untuk Semua Kota Menggunakan xyplot()

Berikut ini syntax dan outup untuk visualisasi dengan Lattice dan Lattice Extra

data <- read.csv("C:/Users/Aura Najwa/Downloads/BINTEL/Data Indeks Kualitas Udara di Indonesia.csv")
View(data)
str(data)
## 'data.frame':    200 obs. of  5 variables:
##  $ Kota    : chr  "Bandung" "Bandung" "Palembang" "Surabaya" ...
##  $ Provinsi: chr  "Jawa Barat" "Jawa Barat" "Sumatera Selatan" "Jawa Timur" ...
##  $ AQI     : int  83 119 120 108 82 66 133 92 166 130 ...
##  $ Kasus   : num  13.9 24 29.6 24.1 19.1 15.5 20.1 26.7 13.7 18.1 ...
##  $ Penduduk: int  11800000 2300000 1600000 4000000 9800000 11200000 3700000 11200000 9100000 6600000 ...
library(lattice)
xyplot(Kasus ~ AQI | Kota,
       data = data,
       groups = Provinsi,
       layout = c(3, 4),
       auto.key = list(title = "Provinsi", space = "right"),
       panel = function(x, y, ..., groups, subscripts) {
         # Titik scatter plot
         panel.xyplot(x, y, ..., groups = groups, subscripts = subscripts)
         
         # Garis regresi
         panel.lmline(x, y, col = "blue")
         
         # Garis batas AQI normal (100)
         panel.abline(v = 100, col = "red", lty = 2)
         
         # Teks anotasi
         panel.text(105, max(y, na.rm = TRUE), "Batas Aman AQI", pos = 4, cex = 0.8, col = "red")
       })

Interpretasi : Dari gambar visualisasi diatas dapat diketahui bahwa terdapat 10 kota, yaitu kota Medan, Palembang, Semaranga, Surabaya, Tangerang, Bandung, Bekasi, Depok, Jakarta, dan Makassar. Dari 10 kota Bekasi dan Makassar cenderung melebihi dari batas aman AQI yang sebesar 100. Sedangkan untuk kota yang cenderung tidak melebihi dari batas aman adalah kota Medan.

library(latticeExtra)
xyplot(Kasus ~ AQI | Kota,
       data = data,
       groups = Provinsi,
       layout = c(3, 4),  
       auto.key = list(title = "Provinsi", space = "right"),
       par.settings = list(superpose.symbol = list(pch = 16, cex = 0.7)),
       panel = function(x, y, ..., groups, subscripts) {
         panel.superpose(x, y, ..., groups = groups, subscripts = subscripts)
         panel.lmline(x, y, col = "blue", lwd = 2)
         panel.abline(v = 100, col = "red", lty = 2)
         panel.text(105, max(y, na.rm = TRUE), "Batas Aman AQI", col = "red", pos = 4, cex = 0.7)
       })

library(ggplot2)
ggplot(data, aes(x = AQI, y = Kasus, color = Provinsi)) +
  geom_point(size = 2, alpha = 0.7) +
  labs(title = "Scatterplot AQI vs Kasus",
       x = "Air Quality Index (AQI)",
       y = "Jumlah Kasus",
       color = "Provinsi") +
  scale_color_brewer(palette = "Set1") +
  theme_minimal()

ggplot(data, aes(x = reorder(Kota, -Penduduk), y = Penduduk, fill = Provinsi)) +
  geom_bar(stat = "identity") +
  labs(title = "Jumlah Penduduk per Kota",
       x = "Kota",
       y = "Penduduk",
       fill = "Provinsi") +
  scale_fill_brewer(palette = "Paired") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

ggplot(data, aes(x = Provinsi, y = AQI, fill = Provinsi)) +
  geom_boxplot() +
  labs(title = "Distribusi AQI Berdasarkan Provinsi",
       x = "Provinsi",
       y = "Air Quality Index (AQI)") +
  scale_fill_brewer(palette = "Set2") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 30, hjust = 1))

scale_fill_manual(values = c("Jawa Timur" = "deeppink", "Jawa Barat" = "darkblue", "DKI Jakarta" = "skyblue"))
## <ggproto object: Class ScaleDiscrete, Scale, gg>
##     aesthetics: fill
##     axis_order: function
##     break_info: function
##     break_positions: function
##     breaks: waiver
##     call: call
##     clone: function
##     dimension: function
##     drop: TRUE
##     expand: waiver
##     get_breaks: function
##     get_breaks_minor: function
##     get_labels: function
##     get_limits: function
##     get_transformation: function
##     guide: legend
##     is_discrete: function
##     is_empty: function
##     labels: waiver
##     limits: function
##     make_sec_title: function
##     make_title: function
##     map: function
##     map_df: function
##     n.breaks.cache: NULL
##     na.translate: TRUE
##     na.value: grey50
##     name: waiver
##     palette: function
##     palette.cache: NULL
##     position: left
##     range: environment
##     rescale: function
##     reset: function
##     train: function
##     train_df: function
##     transform: function
##     transform_df: function
##     super:  <ggproto object: Class ScaleDiscrete, Scale, gg>
ggplot(data, aes(x = AQI, y = Kasus, color = Provinsi)) +
  geom_point(size = 2) +
  labs(title = expression("Scatterplot AQI vs Kasus ("*mu*"g/m"^3*")"),
       x = expression("AQI"),
       y = expression("Jumlah Kasus~(10^3)")) +
  theme_minimal()

ggplot(data, aes(x = Provinsi, y = AQI, fill = Provinsi)) +
  geom_boxplot() +
  labs(title = expression("Boxplot AQI per Provinsi"),
       x = "Provinsi",
       y = expression("Air Quality Index~(AQI)")) +
  theme_minimal()

# Buat palet gradasi
gradasi <- colorRampPalette(c("blue", "green", "yellow", "red"))
n <- length(unique(data$AQI))
warna <- gradasi(n)
# Plot dengan gradasi warna manual
ggplot(data, aes(x = AQI, y = Kasus, color = AQI)) +
  geom_point(size = 2) +
  scale_color_gradientn(colors = warna) +
  labs(title = "Gradasi AQI terhadap Kasus",
       x = expression("AQI"),
       y = "Jumlah Kasus") +
  theme_minimal()