#DISTRIBUSI NORMAL
curve(dnorm(x, mean = 0, sd = 1), 
      from = -4, to = 4, 
      col = "blue", lwd = 2,
      main = "Kurva Distribusi Normal Standard",
      xlab = "Nilai X", ylab = "Kepadatan (Density)")

library(ggplot2)

# 1. Buat rentang nilai X
x <- seq(-5, 7, length.out = 300)

# 2. Buat data frame untuk dua distribusi normal berbeda
df <- data.frame(
  x = rep(x, 2),
  y = c(dnorm(x, mean = 0, sd = 1), dnorm(x, mean = 2, sd = 1.5)),
  Kelompok = rep(c("Normal(0, 1)", "Normal(2, 1.5)"), each = length(x))
)
library(ggplot2)

# 1. Buat rentang nilai X
x <- seq(-6, 8, length.out = 500)

# 2. Buat data frame gabungan 3 distribusi
df <- data.frame(
  x = rep(x, 3),
  y = c(
    dnorm(x, mean = 0, sd = 1),
    dnorm(x, mean = 2, sd = 1),
    dnorm(x, mean = 0, sd = 2)
  ),
  Distribusi = factor(rep(c(
    "N(0, 1) - Standar",
    "N(2, 1) - Geser Mean",
    "N(0, 2) - Variansi Besar"
  ), each = length(x)))
)

# 3. Plot grafik bertumpuk
ggplot(df, aes(x = x, y = y, fill = Distribusi, color = Distribusi)) +
  geom_area(alpha = 0.35, position = "identity", size = 0.8) +
  scale_fill_manual(values = c("#1f77b4", "#ff7f0e", "#2ca02c")) +
  scale_color_manual(values = c("#1f77b4", "#ff7f0e", "#2ca02c")) +
  labs(
    title = "Perbandingan 3 Distribusi Normal",
    x = "Nilai X",
    y = "Kepadatan (Density)"
  ) +
  theme_minimal() +
  theme(legend.position = "top")
## 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.

#DISTRIBUSI T

library(ggplot2)

# Rentang X yang cukup luas agar semua kurva terlihat (dari -4 sampai 16)
x <- seq(-4, 16, length.out = 1000)

# Membuat dataframe untuk 5 distribusi sesuai tabel
df <- data.frame(
  x = rep(x, 5),
  y = c(
    dnorm(x, mean = 0, sd = 1),     # Kurva 1
    dnorm(x, mean = 2, sd = 1),     # Kurva 2
    dnorm(x, mean = 5, sd = 5),     # Kurva 3
    dnorm(x, mean = 10, sd = 2),    # Kurva 4
    dnorm(x, mean = 1.5, sd = 0.1)  # Kurva 5
  ),
  Kurva = factor(rep(c(
    "1: N(0, 1)",
    "2: N(2, 1)",
    "3: N(5, 5)",
    "4: N(10, 2)",
    "5: N(1.5, 0.1)"
  ), each = length(x)))
)

# Plot grafik bertumpuk
ggplot(df, aes(x = x, y = y, fill = Kurva, color = Kurva)) +
  geom_area(alpha = 0.35, position = "identity", size = 0.7) +
  labs(
    title = "Perbandingan 5 Kurva Distribusi Normal",
    x = "Nilai X",
    y = "Kepadatan (Density)"
  ) +
  theme_minimal() +
  theme(legend.position = "right")

library(ggplot2)

ggplot(data.frame(x = c(-4, 4)), aes(x = x)) +
  stat_function(
    fun = dt, 
    args = list(df = 10), 
    geom = "area", 
    fill = "steelblue", 
    alpha = 0.5, 
    color = "darkblue", 
    size = 1
  ) +
  labs(
    title = "Kurva Distribusi t (df = 10)",
    x = "Nilai t",
    y = "Kepadatan (Density)"
  ) +
  theme_minimal()

library(ggplot2)

# 1. Rentang nilai t (sumbu-x)
x <- seq(-4, 4, length.out = 500)

# 2. Buat data frame untuk beberapa nilai df
df_data <- data.frame(
  x = rep(x, 4),
  y = c(
    dt(x, df = 1),
    dt(x, df = 4),
    dt(x, df = 10),
    dt(x, df = 30)
  ),
  Kelompok = factor(rep(c(
    "t (df = 1)",
    "t (df = 4)",
    "t (df = 10)",
    "t (df = 30)"
  ), each = length(x)))
)

# 3. Plot kurva t HANYA GARIS
ggplot(df_data, aes(x = x, y = y, color = Kelompok)) +
  geom_line(size = 1) +  # Hanya garis, tanpa area di dalamnya
  scale_color_manual(values = c("red", "orange", "blue", "darkgreen")) +
  labs(
    title = "Kurva Distribusi t (Garis Tanpa Isian)",
    x = "Nilai t",
    y = "Kepadatan (Density)"
  ) +
  theme_minimal() +
  theme(
    legend.position = "top",
    plot.title = element_text(face = "bold", hjust = 0.5)
  )

#CHI-SQUARE
library(ggplot2)

# 1. Rentang nilai Chi-Square (sumbu-X mulai dari 0)
x <- seq(0, 20, length.out = 500)

# 2. Buat data frame untuk beberapa derajat bebas (df)
df_chi <- data.frame(
  x = rep(x, 4),
  y = c(
    dchisq(x, df = 2),
    dchisq(x, df = 4),
    dchisq(x, df = 6),
    dchisq(x, df = 8)
  ),
  Kelompok = factor(rep(c("df = 2", "df = 4", "df = 6", "df = 8"), each = length(x)))
)

# 3. Plot menggunakan geom_line()
ggplot(df_chi, aes(x = x, y = y, color = Kelompok)) +
  geom_line(size = 1) +  # Cuma garis, gak ada isinya
  scale_color_manual(values = c("red", "orange", "blue", "darkgreen")) +
  labs(
    title = "Kurva Distribusi Chi-Square",
    x = "Nilai Chi-Square",
    y = "Kepadatan (Density)"
  ) +
  theme_minimal() +
  theme(legend.position = "top")

library(ggplot2)

# 1. Menyiapkan rentang nilai X
x_sym <- seq(-4, 4, length.out = 300)      # Untuk distribusi simetris
x_pos <- seq(0.01, 5, length.out = 300)    # Untuk distribusi bernilai positif
x_01  <- seq(0.01, 0.99, length.out = 300) # Untuk distribusi bernilai antara 0 - 1

# 2. Membuat Data Frame Gabungan 10 Distribusi Kontinu
df_kontinu_lengkap <- rbind(
  data.frame(x = x_sym, y = dnorm(x_sym, mean = 0, sd = 1), Distribusi = "1. Normal N(0,1)"),
  data.frame(x = x_sym, y = dt(x_sym, df = 5), Distribusi = "2. t-Student (df=5)"),
  data.frame(x = x_pos, y = dchisq(x_pos, df = 4), Distribusi = "3. Chi-Square (df=4)"),
  data.frame(x = x_pos, y = df(x_pos, df1 = 5, df2 = 10), Distribusi = "4. F-Snedecor (df1=5, df2=10)"),
  data.frame(x = x_pos, y = dexp(x_pos, rate = 1), Distribusi = "5. Eksponensial (rate=1)"),
  data.frame(x = x_pos, y = dgamma(x_pos, shape = 2, rate = 1), Distribusi = "6. Gamma (shape=2, rate=1)"),
  data.frame(x = x_pos, y = dweibull(x_pos, shape = 2, scale = 1), Distribusi = "7. Weibull (shape=2, scale=1)"),
  data.frame(x = x_01, y = dbeta(x_01, shape1 = 2, shape2 = 5), Distribusi = "8. Beta (a=2, b=5)"),
  data.frame(x = x_sym, y = dunif(x_sym, min = -2, max = 2), Distribusi = "9. Uniform (-2, 2)"),
  data.frame(x = x_sym, y = dcauchy(x_sym, location = 0, scale = 1), Distribusi = "10. Cauchy (loc=0, scale=1)")
)

df_kontinu_lengkap$Distribusi <- factor(
  df_kontinu_lengkap$Distribusi, 
  levels = unique(df_kontinu_lengkap$Distribusi)
)

# 3. Plot 10 Panel Grafik
ggplot(df_kontinu_lengkap, aes(x = x, y = y, fill = Distribusi, color = Distribusi)) +
  geom_area(alpha = 0.5, linewidth = 0.6) + 
  facet_wrap(~ Distribusi, scales = "free", ncol = 5) + 
  
  # Warna Isian 
  scale_fill_manual(values = c(
    "lightblue", "lightpink", "lightgreen", "peachpuff", "thistle",
    "lavender", "lightcyan", "moccasin", "wheat", "plum"
  )) +
  
  # Warna Garis Pinggir 
  scale_color_manual(values = c(
    "steelblue", "firebrick", "seagreen", "darkorange", "purple",
    "darkmagenta", "darkcyan", "darkgoldenrod", "burlywood4", "darkorchid"
  )) +
  
  labs(
    title = "Perbandingan Semua Distribusi Kontinu Utama",
    x = "Nilai X",
    y = "Kepadatan (Density)"
  ) +
  theme_minimal() +
  theme(
    legend.position = "none",
    strip.text = element_text(size = 9, face = "bold"),
    plot.title = element_text(face = "bold", hjust = 0.5)
  )