library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.5.3
# ============================================================
# 1. Fungsi Distribusi Pareto
# ============================================================
dpareto <- function(x, xm = 1, alpha = 3){
  ifelse(x >= xm,
         (alpha * xm^alpha)/(x^(alpha+1)),
         0)
}


# ============================================================
# 2. Membuat domain x positif
# ============================================================
x_vals <- seq(0, 15, length.out = 1000)


# ============================================================
# 3. Data Semua Distribusi
# ============================================================
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 (0,5)"
  )
)



# ============================================================
# 4. Urutan Legend
# ============================================================
df_all$Distribusi <- factor(
  df_all$Distribusi,
  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 (0,5)"
  )
)



# ============================================================
# 5. Warna Kurva
# ============================================================
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 (0,5)"="#F59E0B"
)



# ============================================================
# 6. Plot
# ============================================================
ggplot(df_all,
       aes(x=x,
           y=y,
           color=Distribusi,
           fill=Distribusi))+

  geom_area(alpha=0.12,
            position="identity")+

  geom_line(linewidth=1)+

  scale_color_manual(values=warna)+
  scale_fill_manual(values=warna)+

  scale_x_continuous(
    breaks=seq(0,15,2)
  )+

  labs(
    title="Perbandingan 8 Distribusi Probabilitas Kontinu",
    subtitle="Visualisasi Fungsi Kepadatan Probabilitas (PDF)",
    x="Nilai Variabel (x)",
    y="Kepadatan Probabilitas f(x)",
    color="Distribusi",
    fill="Distribusi"
  )+

  theme_minimal(base_size=12)+

  theme(
    plot.title=element_text(
      face="bold",
      size=15
    ),

    legend.position="right",

    legend.title=
      element_text(face="bold"),

    panel.grid.minor=
      element_blank()
  )

library(ggplot2)

# ============================================================
# 1. Domain x masing-masing distribusi
# ============================================================

x_t <- seq(-4, 4, length.out = 500)
x_f <- seq(0.01, 10, length.out = 500)
x_chi <- seq(0, 15, length.out = 500)
x_gamma <- seq(0, 10, length.out = 500)
x_weibull <- seq(0, 5, length.out = 500)
x_exp <- seq(0, 5, length.out = 500)
x_pareto <- seq(1, 5, length.out = 500)
x_unif <- seq(0, 5, length.out = 500)


# ============================================================
# 2. Fungsi Pareto
# ============================================================

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


# ============================================================
# 3. Data Gabungan
# ============================================================

df_all <- rbind(

data.frame(
 x=x_t,
 y=dt(x_t,df=5),
 Distribusi="t-Student (df=5)"
),

data.frame(
 x=x_f,
 y=df(x_f,df1=5,df2=10),
 Distribusi="F (df1=5, df2=10)"
),

data.frame(
 x=x_chi,
 y=dchisq(x_chi,df=4),
 Distribusi="Chi-Square (df=4)"
),

data.frame(
 x=x_gamma,
 y=dgamma(x_gamma,shape=2,rate=1),
 Distribusi="Gamma (shape=2, rate=1)"
),

data.frame(
 x=x_weibull,
 y=dweibull(x_weibull,shape=2,scale=1),
 Distribusi="Weibull (shape=2, scale=1)"
),

data.frame(
 x=x_exp,
 y=dexp(x_exp,rate=1),
 Distribusi="Eksponensial (rate=1)"
),

data.frame(
 x=x_pareto,
 y=dpareto(x_pareto,xm=1,alpha=3),
 Distribusi="Pareto (xm=1, alpha=3)"
),

data.frame(
 x=x_unif,
 y=dunif(x_unif,min=0,max=5),
 Distribusi="Uniform (0,5)"
)

)



# ============================================================
# 4. Warna tiap distribusi
# ============================================================

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 (0,5)"="#F59E0B"
)



# ============================================================
# 5. Plot Facet 8 Kotak
# ============================================================

ggplot(df_all,
       aes(
         x=x,
         y=y,
         color=Distribusi,
         fill=Distribusi
       ))+

geom_area(
 alpha=0.25
)+

geom_line(
 linewidth=1
)+


scale_color_manual(
 values=warna
)+

scale_fill_manual(
 values=warna
)+


facet_wrap(
 ~Distribusi,
 scales="free",
 ncol=4
)+


labs(
 title="Perbandingan 8 Distribusi Probabilitas Kontinu",
 subtitle="Visualisasi Fungsi Kepadatan Probabilitas (PDF)",
 x="Nilai Variabel 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
 ),

plot.subtitle=
 element_text(
  hjust=0.5
 ),

legend.position="none"

)