# Unduh dan pasang paket jika belum terinstal di perangkat:
# install.packages("ggplot2")
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.5.2
# Menggambar grafik kurva menggunakan fungsi stat_function
ggplot(data.frame(x = c(-4, 4)), aes(x = x)) +
  stat_function(fun = dnorm, 
                args = list(mean = 0, sd = 1), 
                color = "blue", 
                linewidth = 1) +
  labs(title = "Kurva Distribusi Normal",
       subtitle = "Rata-rata = 0, Simpangan Baku = 1",
       x = "Nilai",
       y = "Kepadatan") +
  theme_minimal() # Tema latar belakang bersih

library(ggplot2)
ggplot(data.frame(x = c(-8, 12)), aes(x = x)) +
  stat_function(fun = dnorm, args = list(mean = 2, sd = sqrt(10)), color = "blue", linewidth = 1) +
  labs(title = "Kurva Distribusi Normal", subtitle = expression(paste("Rata-rata (", mu, ") = 2, Varians (", sigma^2, ") = 10")), x = "Nilai", y = "Kepadatan") +
  theme_minimal()

library(ggplot2)

ggplot(data.frame(x = c(-8, 12)), aes(x = x)) +
  stat_function(fun = dnorm, 
                args = list(mean = 2, sd = sqrt(10)), 
                color = "blue", 
                linewidth = 1) +
  labs(title = "Kurva Distribusi Normal",
       subtitle = expression(paste("Rata-rata (", mu, ") = 2, Varians (", sigma^2, ") = 10")),
       x = "Nilai",
       y = "Kepadatan") +
  theme_minimal()

library(ggplot2)

ggplot(data.frame(x = c(-8, 12)), aes(x = x)) +
  # Kurva Pertama
  stat_function(aes(color = "Distribusi 1 (Mu = 0, Var = 1)"),
                fun = dnorm, 
                args = list(mean = 0, sd = 1), 
                linewidth = 1) +
  # Kurva Kedua
  stat_function(aes(color = "Distribusi 2 (Mu = 2, Var = 10)"),
                fun = dnorm, 
                args = list(mean = 2, sd = sqrt(10)), 
                linewidth = 1) +
  # Atur warna manual untuk legenda
  scale_color_manual(name = "Keterangan Parameter",
                     values = c("Distribusi 1 (Mu = 0, Var = 1)" = "red", 
                                "Distribusi 2 (Mu = 2, Var = 10)" = "blue")) +
  labs(title = "Perbandingan Dua Kurva Distribusi Normal",
       x = "Nilai",
       y = "Kepadatan") +
  theme_minimal() +
  theme(legend.position = "bottom") # Pindahkan legenda ke bawah

library(ggplot2)

ggplot(data.frame(x = c(-10, 12)), aes(x = x)) +
  # Kurva Pertama
  stat_function(aes(color = "Distribusi 1 (Mu = 0, Var = 1)"),
                fun = dnorm, 
                args = list(mean = 0, sd = 1), 
                linewidth = 1) +
  # Kurva Kedua
  stat_function(aes(color = "Distribusi 2 (Mu = 2, Var = 10)"),
                fun = dnorm, 
                args = list(mean = 2, sd = sqrt(10)), 
                linewidth = 1) +
  # Kurva Ketiga
  stat_function(aes(color = "Distribusi 3 (Mu = -3, Var = 4)"),
                fun = dnorm, 
                args = list(mean = -3, sd = 2), 
                linewidth = 1) +
  # Atur warna manual untuk legenda
  scale_color_manual(name = "Keterangan Parameter",
                     values = c("Distribusi 1 (Mu = 0, Var = 1)" = "red", 
                                "Distribusi 2 (Mu = 2, Var = 10)" = "blue",
                                "Distribusi 3 (Mu = -3, Var = 4)" = "green")) +
  labs(title = "Perbandingan Tiga Kurva Distribusi Normal",
       x = "Nilai",
       y = "Kepadatan") +
  theme_minimal() +
  theme(legend.position = "bottom")

library(ggplot2)

ggplot(data.frame(x = c(-10, 25)), aes(x = x)) +
  stat_function(aes(color = "Kurva 1 (Mu=0, Sig=1)"),
                fun = dnorm, args = list(mean = 0, sd = 1), linewidth = 1) +
  stat_function(aes(color = "Kurva 2 (Mu=2, Sig=1)"),
                fun = dnorm, args = list(mean = 2, sd = 1), linewidth = 1) +
  stat_function(aes(color = "Kurva 3 (Mu=5, Sig=5)"),
                fun = dnorm, args = list(mean = 5, sd = 5), linewidth = 1) +
  stat_function(aes(color = "Kurva 4 (Mu=10, Sig=1)"),
                fun = dnorm, args = list(mean = 10, sd = 1), linewidth = 1) +
  stat_function(aes(color = "Kurva 5 (Mu=15, Sig=0.1)"),
                fun = dnorm, args = list(mean = 15, sd = 0.1), linewidth = 1) +
  scale_color_manual(name = "Keterangan Parameter",
                     values = c("Kurva 1 (Mu=0, Sig=1)" = "red", 
                                "Kurva 2 (Mu=2, Sig=1)" = "blue",
                                "Kurva 3 (Mu=5, Sig=5)" = "green",
                                "Kurva 4 (Mu=10, Sig=1)" = "purple",
                                "Kurva 5 (Mu=15, Sig=0.1)" = "orange")) +
  labs(title = "Perbandingan 5 Kurva Distribusi Normal",
       x = "Nilai",
       y = "Kepadatan") +
  theme_minimal() +
  theme(legend.position = "right")

# Parameter (mu dan sd)
params <- list(
  c(mu = 0,   sd = 2,   col = "blue"), # Biru
  c(mu = 2,   sd = 1,   col = "green"), # Hijau
  c(mu = 5,   sd = 5,   col = "red"), # Merah
  c(mu = 10,  sd = 3,   col = "purple"), # Ungu
  c(mu = 1.5, sd = 0.1, col = "orange")  # Oranye
)

# Rentang sumbu X
x <- seq(-11, 20, length.out = 2000)

# Plot Kurva 1 sebagai dasar
y1 <- dnorm(x, mean = 0, sd = 2)
plot(x, y1, type = "l", col = "#1F77B4", lwd = 2,
     main = "Perbandingan 5 Distribusi Normal Bertumpuk",
     xlab = "Nilai X", ylab = "Densitas",
     ylim = c(0, 1.2), # Membatasi sumbu Y agar kurva lain tetap terlihat jelas
     las = 1, bty = "l")

# Menambahkan kurva sisanya dengan perulangan (loop)
for (p in params) {
  m <- as.numeric(p["mu"])
  s <- as.numeric(p["sd"])
  cl <- p["col"]
  
  y <- dnorm(x, mean = m, sd = s)
  lines(x, y, col = cl, lwd = 2)
  polygon(c(x, rev(x)), c(y, rep(0, length(y))), 
          col = adjustcolor(cl, alpha.f = 0.15), border = NA)
}

grid()
legend("topright", 
       legend = c("N(0, sd=2)", "N(2, sd=1)", "N(5, sd=5)", "N(10, sd=3)", "N(1.5, sd=0.1)"),
       col = c("#1F77B4", "#2CA02C", "#D62728", "#9467BD", "#FF7F0E"), 
       lwd = 2, bty = "n")

library(ggplot2)

# Kurva 1
ggplot(data.frame(x = c(-3, 3)), aes(x = x)) +
  stat_function(fun = dnorm, args = list(mean = 0, sd = 1), color = "red", linewidth = 1) +
  labs(title = "Kurva 1 (Mu=0, Sig=1)") + theme_minimal()

# Kurva 2
ggplot(data.frame(x = c(-1, 5)), aes(x = x)) +
  stat_function(fun = dnorm, args = list(mean = 2, sd = 1), color = "blue", linewidth = 1) +
  labs(title = "Kurva 2 (Mu=2, Sig=1)") + theme_minimal()

# Kurva 3
ggplot(data.frame(x = c(-10, 20)), aes(x = x)) +
  stat_function(fun = dnorm, args = list(mean = 5, sd = 5), color = "green", linewidth = 1) +
  labs(title = "Kurva 3 (Mu=5, Sig=5)") + theme_minimal()

# Kurva 4
ggplot(data.frame(x = c(7, 13)), aes(x = x)) +
  stat_function(fun = dnorm, args = list(mean = 10, sd = 1), color = "purple", linewidth = 1) +
  labs(title = "Kurva 4 (Mu=10, Sig=1)") + theme_minimal()

# Kurva 5
ggplot(data.frame(x = c(14.7, 15.3)), aes(x = x)) +
  stat_function(fun = dnorm, args = list(mean = 15, sd = 0.1), color = "orange", linewidth = 1) +
  labs(title = "Kurva 5 (Mu=15, Sig=0.1)") + theme_minimal()

# 1. Muat paket yang diperlukan
# install.packages(c("ggplot2", "dplyr", "patchwork")) # Jalankan ini jika belum install
library(ggplot2)
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.5.3
## 
## 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(patchwork)
## Warning: package 'patchwork' was built under R version 4.5.3
# ==========================================
# GRAFIK 1: DISTRIBUSI BINOMIAL (Barchart)
# Mirip dengan grafik kiri atas di gambar
# ==========================================
df_binom <- data.frame(
  x = rep(0:20, 3),
  # Membuat 3 variasi probabilitas (p)
  p = factor(rep(c(0.2, 0.5, 0.8), each = 21)), 
  prob = c(dbinom(0:20, size = 20, prob = 0.2), 
           dbinom(0:20, size = 20, prob = 0.5), 
           dbinom(0:20, size = 20, prob = 0.8))
)

plot_binom <- ggplot(df_binom, aes(x = x, y = prob, fill = p)) +
  geom_bar(stat = "identity", position = "dodge", alpha = 0.8) +
  scale_fill_brewer(palette = "Set2") +
  labs(title = "Distribusi Binomial", subtitle = "n = 20", 
       x = "Jumlah Sukses", y = "Probabilitas", fill = "Nilai p") +
  theme_minimal() +
  theme(legend.position = "right")

# ==========================================
# GRAFIK 2: DISTRIBUSI POISSON (Lollipop Chart)
# Mirip dengan grafik kanan atas di gambar
# ==========================================
df_poisson <- data.frame(
  x = rep(0:15, 3),
  # Membuat 3 variasi Lambda
  lambda = factor(rep(c(1, 4, 8), each = 16)),
  prob = c(dpois(0:15, lambda = 1), 
           dpois(0:15, lambda = 4), 
           dpois(0:15, lambda = 8))
)

plot_poisson <- ggplot(df_poisson, aes(x = x, y = prob, color = lambda)) +
  geom_segment(aes(xend = x, yend = 0), linewidth = 1, alpha = 0.5) +
  geom_point(size = 2) +
  scale_color_brewer(palette = "Set1") +
  labs(title = "Distribusi Poisson", subtitle = "Variasi Lambda", 
       x = "Jumlah Kejadian", y = "Probabilitas", color = "Lambda") +
  theme_minimal()

# ==========================================
# GRAFIK 3: DISTRIBUSI t-STUDENT (Line Chart)
# ==========================================
df_t <- data.frame(x = c(-4, 4))

plot_t <- ggplot(df_t, aes(x = x)) +
  stat_function(aes(color = "df = 1"), fun = dt, args = list(df = 1), linewidth = 1) +
  stat_function(aes(color = "df = 5"), fun = dt, args = list(df = 5), linewidth = 1) +
  stat_function(aes(color = "df = 30 (Mendekati Normal)"), fun = dt, args = list(df = 30), 
                linewidth = 1, linetype = "dashed") +
  scale_color_manual(values = c("red", "blue", "black")) +
  labs(title = "Distribusi t-Student", subtitle = "Berdasarkan Derajat Bebas (df)",
       x = "Nilai t", y = "Kepadatan", color = "Parameter") +
  theme_minimal()

# ==========================================
# GRAFIK 4: DISTRIBUSI F (Line Chart)
# ==========================================
df_f <- data.frame(x = c(0, 5))

plot_f <- ggplot(df_f, aes(x = x)) +
  # Distribusi F memiliki 2 derajat bebas (df1 dan df2)
  stat_function(aes(color = "df1=2, df2=5"), fun = df, args = list(df1=2, df2=5), linewidth = 1) +
  stat_function(aes(color = "df1=5, df2=10"), fun = df, args = list(df1=5, df2=10), linewidth = 1) +
  stat_function(aes(color = "df1=10, df2=50"), fun = df, args = list(df1=10, df2=50), linewidth = 1) +
  scale_color_manual(values = c("orange", "purple", "darkgreen")) +
  labs(title = "Distribusi F (Fisher-Snedecor)", subtitle = "Berdasarkan df1 dan df2",
       x = "Nilai F", y = "Kepadatan", color = "Parameter") +
  theme_minimal()

# ==========================================
# MENGGABUNGKAN GRAFIK MENJADI DASHBOARD (Menggunakan Patchwork)
# ==========================================
# Operator | untuk meletakkan bersebelahan, / untuk meletakkan di bawahnya
dashboard_visual <- (plot_binom | plot_poisson) / (plot_t | plot_f)

# Tampilkan hasil akhir
print(dashboard_visual)

# Muat paket yang diperlukan
library(ggplot2)
library(patchwork)

# ==========================================
# 1. DISTRIBUSI CHI-SQUARE
# Parameter utama: df (derajat bebas)
# ==========================================
plot_chisq <- ggplot(data.frame(x = c(0, 20)), aes(x = x)) +
  stat_function(aes(color = "df = 2"), fun = dchisq, args = list(df = 2), linewidth = 1) +
  stat_function(aes(color = "df = 5"), fun = dchisq, args = list(df = 5), linewidth = 1) +
  stat_function(aes(color = "df = 10"), fun = dchisq, args = list(df = 10), linewidth = 1) +
  scale_color_manual(values = c("red", "blue", "green")) +
  labs(title = "Distribusi Chi-Square", x = "Nilai", y = "Kepadatan", color = "Parameter") +
  theme_minimal()

# ==========================================
# 2. DISTRIBUSI GAMMA
# Parameter utama: shape (bentuk) dan rate/scale (skala)
# ==========================================
plot_gamma <- ggplot(data.frame(x = c(0, 15)), aes(x = x)) +
  stat_function(aes(color = "Shape=1, Rate=1"), fun = dgamma, args = list(shape = 1, rate = 1), linewidth = 1) +
  stat_function(aes(color = "Shape=2, Rate=1"), fun = dgamma, args = list(shape = 2, rate = 1), linewidth = 1) +
  stat_function(aes(color = "Shape=5, Rate=1"), fun = dgamma, args = list(shape = 5, rate = 1), linewidth = 1) +
  scale_color_manual(values = c("orange", "purple", "brown")) +
  labs(title = "Distribusi Gamma", x = "Nilai", y = "Kepadatan", color = "Parameter") +
  theme_minimal()

# ==========================================
# 3. DISTRIBUSI WEIBULL
# Parameter utama: shape (bentuk) dan scale (skala)
# ==========================================
plot_weibull <- ggplot(data.frame(x = c(0, 5)), aes(x = x)) +
  stat_function(aes(color = "Shape=1, Scale=1"), fun = dweibull, args = list(shape = 1, scale = 1), linewidth = 1) +
  stat_function(aes(color = "Shape=2, Scale=1"), fun = dweibull, args = list(shape = 2, scale = 1), linewidth = 1) +
  stat_function(aes(color = "Shape=5, Scale=1"), fun = dweibull, args = list(shape = 5, scale = 1), linewidth = 1) +
  scale_color_manual(values = c("cyan", "magenta", "black")) +
  labs(title = "Distribusi Weibull", x = "Nilai", y = "Kepadatan", color = "Parameter") +
  theme_minimal()

# ==========================================
# 4. DISTRIBUSI EKSPONENSIAL
# Parameter utama: rate (laju)
# ==========================================
plot_exp <- ggplot(data.frame(x = c(0, 5)), aes(x = x)) +
  stat_function(aes(color = "Rate = 0.5"), fun = dexp, args = list(rate = 0.5), linewidth = 1) +
  stat_function(aes(color = "Rate = 1.0"), fun = dexp, args = list(rate = 1), linewidth = 1) +
  stat_function(aes(color = "Rate = 2.0"), fun = dexp, args = list(rate = 2), linewidth = 1) +
  scale_color_manual(values = c("darkred", "darkblue", "darkgreen")) +
  labs(title = "Distribusi Eksponensial", x = "Nilai", y = "Kepadatan", color = "Parameter") +
  theme_minimal()

# ==========================================
# 5. DISTRIBUSI PARETO
# R tidak memiliki dpareto bawaan, jadi kita definisikan fungsinya
# ==========================================
dpareto <- function(x, scale, shape) {
  # Mengembalikan 0 jika nilai x lebih kecil dari skala awal (xm)
  ifelse(x < scale, 0, (shape * scale^shape) / (x^(shape + 1)))
}

plot_pareto <- ggplot(data.frame(x = c(0, 5)), aes(x = x)) +
  stat_function(aes(color = "Scale=1, Shape=1"), fun = dpareto, args = list(scale = 1, shape = 1), linewidth = 1) +
  stat_function(aes(color = "Scale=1, Shape=2"), fun = dpareto, args = list(scale = 1, shape = 2), linewidth = 1) +
  stat_function(aes(color = "Scale=1, Shape=3"), fun = dpareto, args = list(scale = 1, shape = 3), linewidth = 1) +
  scale_color_manual(values = c("gold", "tomato", "steelblue")) +
  labs(title = "Distribusi Pareto", x = "Nilai", y = "Kepadatan", color = "Parameter") +
  theme_minimal()

# ==========================================
# MENGGABUNGKAN GRAFIK (Dashboard Layout)
# ==========================================
# Mengatur 5 grafik ke dalam 3 baris. Baris terakhir untuk Pareto saja.
dashboard_lanjutan <- (plot_chisq | plot_gamma) / 
                      (plot_weibull | plot_exp) / 
                      plot_pareto

# Menampilkan hasil
print(dashboard_lanjutan)

library(ggplot2)
library(patchwork)

# ==========================================
# 1. DISTRIBUSI UNIFORM DISKRIT (Seperti di gambar)
# Contoh kasus: Pelemparan mata dadu (1 sampai 6)
# ==========================================
# Membuat dataframe dengan probabilitas yang sama rata (1/6)
df_uniform_diskrit <- data.frame(
  x = 1:6,
  prob = rep(1/6, 6)
)

plot_unif_diskrit <- ggplot(df_uniform_diskrit, aes(x = factor(x), y = prob)) +
  geom_bar(stat = "identity", fill = "skyblue", alpha = 0.9, width = 0.6) +
  # Mengatur batas sumbu y agar proporsional
  scale_y_continuous(limits = c(0, 0.25)) + 
  labs(title = "Distribusi Uniform Diskrit", 
       subtitle = "Contoh: Hasil lemparan dadu (1-6)",
       x = "Keluaran (x)", 
       y = "Probabilitas (P(X=x))") +
  theme_minimal()

# ==========================================
# 2. DISTRIBUSI UNIFORM KONTINU
# Parameter utama: min (batas bawah) dan max (batas atas)
# ==========================================
# Membuat fungsi untuk menggambar garis seragam
plot_unif_kontinu <- ggplot(data.frame(x = c(-2, 12)), aes(x = x)) +
  # Rentang dari 0 hingga 10
  stat_function(aes(color = "Min = 0, Max = 10"), 
                fun = dunif, args = list(min = 0, max = 10), linewidth = 1.2) +
  # Rentang dari 2 hingga 8
  stat_function(aes(color = "Min = 2, Max = 8"), 
                fun = dunif, args = list(min = 2, max = 8), linewidth = 1.2, linetype = "dashed") +
  scale_color_manual(values = c("Min = 0, Max = 10" = "blue", "Min = 2, Max = 8" = "red")) +
  labs(title = "Distribusi Uniform Kontinu", 
       subtitle = "Berdasarkan rentang Minimum dan Maksimum",
       x = "Nilai", 
       y = "Kepadatan (Density)", 
       color = "Parameter") +
  theme_minimal() +
  theme(legend.position = "bottom")

# ==========================================
# MENGGABUNGKAN GRAFIK
# ==========================================
# Meletakkan grafik secara berdampingan (kiri dan kanan)
dashboard_uniform <- plot_unif_diskrit | plot_unif_kontinu

# Menampilkan grafik
print(dashboard_uniform)

library(ggplot2)

# 1. Set data x
x_val <- seq(0.01, 10, length.out = 500)

# 2. Rumus manual Pareto Tipe I
dpareto <- function(x, xm = 1, alpha = 3) {
  ifelse(x >= xm, (alpha * (xm^alpha)) / (x^(alpha + 1)), 0)
}

# 3. Gabungkan data 8 distribusi kontinu
df_all <- rbind(
  data.frame(x = seq(-4, 4, length.out = 500), y = dt(seq(-4, 4, length.out = 500), df = 5), Distribusi = "t-Student (df=5)"),
  data.frame(x = x_val, y = df(x_val, df1 = 5, df2 = 10), Distribusi = "F (df1=5, df2=10)"),
  data.frame(x = seq(0, 15, length.out = 500), y = dchisq(seq(0, 15, length.out = 500), df = 4), Distribusi = "Chi-Square (df=4)"),
  data.frame(x = x_val, y = dgamma(x_val, shape = 2, rate = 1), Distribusi = "Gamma (shape=2, rate=1)"),
  data.frame(x = seq(0, 4, length.out = 500), y = dweibull(seq(0, 4, length.out = 500), shape = 2, scale = 1), Distribusi = "Weibull (shape=2, scale=1)"),
  data.frame(x = seq(0, 5, length.out = 500), y = dexp(seq(0, 5, length.out = 500), rate = 1), Distribusi = "Eksponensial (rate=1)"),
  data.frame(x = seq(1, 5, length.out = 500), y = dpareto(seq(1, 5, length.out = 500), xm = 1, alpha = 3), Distribusi = "Pareto (xm=1, alpha=3)"),
  data.frame(x = seq(-1, 6, length.out = 500), y = dunif(seq(-1, 6, length.out = 500), min = 0, max = 5), Distribusi = "Uniform (0,5)")
)

# 4. Plot gabungan dengan Facet (dipisah per panel kecil)
ggplot(df_all, aes(x = x, y = y, color = Distribusi, fill = Distribusi)) +
  geom_area(alpha = 0.25, show.legend = FALSE) +
  geom_line(size = 1, show.legend = FALSE) +
  facet_wrap(~ Distribusi, scales = "free", ncol = 4) + # Membagi jadi 8 kotak kecil (2 baris x 4 kolom)
  labs(
    title = "Perbandingan 8 Distribusi Kontinu",
    x = "Nilai X",
    y = "Kerapatan Probabilitas f(x)"
  ) +
  theme_bw() +
  theme(
    strip.background = element_rect(fill = "#E9ECEF"),
    strip.text = element_text(face = "bold", size = 9),
    plot.title = element_text(face = "bold", size = 14, hjust = 0.5)
  )
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

library(ggplot2)

# ------------------------------------------------------------
# 1. Definisi Fungsi Pareto
# ------------------------------------------------------------
dpareto <- function(x, xm = 1, alpha = 3) {
  ifelse(x >= xm, (alpha * (xm^alpha)) / (x^(alpha + 1)), 0)
}

# ------------------------------------------------------------
# 2. Sumbu X Bersama (-4 sampai 15 agar mencakup semua domain)
# ------------------------------------------------------------
x_vals <- seq(-4, 15, length.out = 1000)

# ------------------------------------------------------------
# 3. Membuat Data Frame Gabungan (Tidy Format)
# ------------------------------------------------------------
df_all <- rbind(
  data.frame(x = x_vals, y = dt(x_vals, df = 5),                   Distribusi = "t-Student (df=5)"),
  data.frame(x = x_vals, y = df(x_vals, df1 = 5, df2 = 10),        Distribusi = "F (df1=5, df2=10)"),
  data.frame(x = x_vals, y = dchisq(x_vals, df = 4),               Distribusi = "Chi-Square (df=4)"),
  data.frame(x = x_vals, y = dgamma(x_vals, shape = 2, rate = 1),  Distribusi = "Gamma (shape=2, rate=1)"),
  data.frame(x = x_vals, y = dweibull(x_vals, shape = 2, scale = 1),Distribusi = "Weibull (shape=2, scale=1)"),
  data.frame(x = x_vals, y = dexp(x_vals, rate = 1),               Distribusi = "Eksponensial (rate=1)"),
  data.frame(x = x_vals, y = dpareto(x_vals, xm = 1, alpha = 3),   Distribusi = "Pareto (xm=1, alpha=3)"),
  data.frame(x = x_vals, y = dunif(x_vals, min = 0, max = 5),       Distribusi = "Uniform (min=0, max=5)")
)

# Mengunci urutan label di legenda sesuai keinginan Anda
dist_levels <- c(
  "t-Student (df=5)", "F (df1=5, df2=10)", "Chi-Square (df=4)", 
  "Gamma (shape=2, rate=1)", "Weibull (shape=2, scale=1)", 
  "Eksponensial (rate=1)", "Pareto (xm=1, alpha=3)", "Uniform (min=0, max=5)"
)
df_all$Distribusi <- factor(df_all$Distribusi, levels = dist_levels)

# ------------------------------------------------------------
# 4. Palet Warna Kustom (Sesuai Hex Code Kode Asli Anda)
# ------------------------------------------------------------
palet_warna <- c(
  "t-Student (df=5)"        = "#4361EE",
  "F (df1=5, df2=10)"       = "#F72585",
  "Chi-Square (df=4)"       = "#4CC9F0",
  "Gamma (shape=2, rate=1)"  = "#7209B7",
  "Weibull (shape=2, scale=1)" = "#3A0CA3",
  "Eksponensial (rate=1)"   = "#4895EF",
  "Pareto (xm=1, alpha=3)"  = "#10B981",
  "Uniform (min=0, max=5)"  = "#F59E0B"
)

# ------------------------------------------------------------
# 5. Visualisasi 1 Frame
# ------------------------------------------------------------
ggplot(df_all, aes(x = x, y = y, color = Distribusi, fill = Distribusi)) +
  
  # Layer 1: Isian area transparan bertumpuk
  geom_area(alpha = 0.12, position = "identity") +
  
  # Layer 2: Garis utama kurva
  geom_line(linewidth = 0.9) +
  
  # Layer 3: Pemetaan Warna Kustom
  scale_color_manual(values = palet_warna) +
  scale_fill_manual(values = palet_warna) +
  
  # Layer 4: Penyesuaian Skala
  # Batasi Y hingga 1.2 agar kurva Pareto (puncak y=3) tidak memipihkan kurva lainnya
  coord_cartesian(ylim = c(0, 1.2)) + 
  scale_x_continuous(breaks = seq(-4, 15, by = 2)) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.05))) +
  
  # Layer 5: Judul dan Label
  labs(
    title = "Perbandingan 8 Distribusi Kontinu",
    subtitle = "Digabungkan dalam Satu Sumbu Koordinat",
    x = "Nilai Variable (x)",
    y = "Kepadatan Probabilitas f(x)",
    color = "Jenis Distribusi",
    fill  = "Jenis Distribusi",
    caption = "Dibuat dengan R & ggplot2"
  ) +
  
  # Layer 6: Styling Tema
  theme_minimal(base_size = 12) +
  theme(
    plot.title = element_text(face = "bold", size = 15, color = "#111827"),
    plot.subtitle = element_text(color = "#4B5563", margin = margin(b = 12)),
    axis.title = element_text(face = "bold", color = "#374151"),
    
    # Pengaturan Legenda di Samping
    legend.position = "right",
    legend.title = element_text(face = "bold", size = 10),
    legend.background = element_rect(fill = "#F9FAFB", color = "#E5E7EB", linewidth = 0.5),
    legend.margin = margin(6, 10, 6, 10),
    
    panel.grid.minor = element_blank(),
    panel.grid.major = element_line(color = "#F3F4F6")
  )