#distribusi normal
library(ggplot2)# Buat data garis kurva lonceng
## Warning: package 'ggplot2' was built under R version 4.5.3
x <- seq(-4, 4, length.out = 300)
y <- dnorm(x)
df <- data.frame(x, y)# Plot kurva lonceng
ggplot(df, aes(x = x, y = y)) +
geom_area(fill = "steelblue", alpha = 0.3) + # Isian warna di dalam lonceng
geom_line(color = "navy", size = 1.2) + # Garis kurva lonceng
labs(title = "Kurva Lonceng (Distribusi Normal)",
x = "Nilai (x)", y = "Kerapatan (Density)") +
theme_minimal()
## 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)
mu <- 10
sd <- sqrt(2) # sd = 1.414# Rentang sumbu x (sekitar mu +/- 4*sd agar loncengnya pas)
x <- seq(mu - 4*sd, mu + 4*sd, length.out = 300)
y <- dnorm(x, mean = mu, sd = sd)
df <- data.frame(x, y)
ggplot(df, aes(x = x, y = y)) +
geom_area(fill = "tomato", alpha = 0.3) +
geom_line(color = "seagreen", size = 1.2) +
geom_vline(xintercept = mu, linetype = "dashed", color = "steelblue") + # Garis tengah mu = 10
labs(title = "Kurva Normal (mu = 10, variansi = 2)",
x = "x", y = "Density") +
theme_minimal()

# 1. Buat grid data kontinu (x kontinu, pakai titik-titik sampel)
x <- seq(0, 25, by = 1) # Titik-titik sampel x
# Hitung PDF Distribusi Gamma untuk 3 variasi parameter (Shape & Rate)
# agar menghasilkan 3 bentuk kurva persis seperti gaya di foto
df_gamma <- rbind(
data.frame(x = x, y = dgamma(x, shape = 1, rate = 0.2), Label = "rate = 0.2"),
data.frame(x = x, y = dgamma(x, shape = 3, rate = 0.5), Label = "rate = 0.4"),
data.frame(x = x, y = dgamma(x, shape = 6, rate = 1.0), Label = "rate = 0.6")
)
# 2. Plot dengan style yang persis sama seperti di foto slide
ggplot(df_gamma, aes(x = x, y = y, color = Label, group = Label)) +
# Garis mulus kontinu + titik-titik sampel
geom_line(size = 1.1) +
geom_point(size = 2.2) +
# Judul & Label Sumbu
labs(
title = "Perbandingan Distribusi Gamma (Kontinu)",
subtitle = "Variasi Parameter dengan Bentuk Kurva Berbeda",
x = "x",
y = "f(x)",
color = NULL
) +
# Skalasi Sumbu
scale_x_continuous(breaks = seq(0, 25, by = 5)) +
# Skema Warna Sesuai Gambar (Ungu/Merah Tua, Biru, Hijau)
scale_color_manual(values = c("rate = 0.2" = "#8B1E3F",
"rate = 0.4" = "#2B59C3",
"rate = 0.6" = "#2A9D8F")) +
# Styling Tema Pas Sesuai Layout Foto
theme_minimal() +
theme(
plot.title = element_text(face = "bold", size = 14, color = "#222222"),
plot.subtitle = element_text(size = 10, color = "#666666", margin = margin(b = 10)),
axis.title = element_text(size = 11, color = "#444444"),
# Legenda di posisi kiri atas (sesuai foto)
legend.position = c(0.12, 0.65),
legend.background = element_rect(fill = "white", color = NA),
legend.text = element_text(size = 10, face = "bold"),
# Grid tipis khas ggplot
panel.grid.minor = element_blank(),
panel.grid.major = element_line(color = "#E5E5E5", size = 0.5)
)
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

library(ggplot2)
# 1. Buat sequence x yang cukup lebar agar semua kurva terekam
x_val <- seq(-5, 20, length.out = 1000)
# 2. Definisikan parameter dari tabel Excel
params <- data.frame(
kurva = factor(1:5),
mu = c(0, 2, 5, 10, 15),
sigma = c(1, 1, 5, 3, 0.1)
)
# 3. Hitung nilai density untuk tiap kombinasi parameter
df_list <- lapply(1:nrow(params), function(i) {
data.frame(
x = x_val,
y = dnorm(x_val, mean = params$mu[i], sd = params$sigma[i]),
Label = paste0("Kurva ", params$kurva[i], ": mu=", params$mu[i], ", sigma=", params$sigma[i])
)
})
df_plot <- do.call(rbind, df_list)
# 4. Plot grafik perbandingan
ggplot(df_plot, aes(x = x, y = y, color = Label)) +
geom_line(size = 1) +
labs(
title = "Simulasi Distribusi Normal",
subtitle = "Perbandingan berbagai nilai Rata-Rata (Mu) dan Simpangan Baku (Sigma)",
x = "Nilai X",
y = "Kerapatan Probabilitas f(x)",
color = "Parameter"
) +
theme_minimal() +
theme(
plot.title = element_text(face = "bold", size = 14),
legend.position = "right"
)

library(ggplot2)
library(gridExtra)
## Warning: package 'gridExtra' was built under R version 4.5.3
# ------------------------------------------------------------
# 1. Distribusi t-Student (df = 5)
# ------------------------------------------------------------
x1 <- seq(-4, 4, length.out = 300)
df1 <- data.frame(x = x1, y = dt(x1, df = 5))
p1 <- ggplot(df1, aes(x, y)) +
geom_line(color = "#4361EE", size = 1) +
geom_area(fill = "#4361EE", alpha = 0.2) +
labs(title = "t-Student (df = 5)", x = "x", y = "f(x)") +
theme_minimal()
# ------------------------------------------------------------
# 2. Distribusi F (df1 = 5, df2 = 10)
# ------------------------------------------------------------
x2 <- seq(0, 5, length.out = 300)
df2 <- data.frame(x = x2, y = df(x2, df1 = 5, df2 = 10))
p2 <- ggplot(df2, aes(x, y)) +
geom_line(color = "#F72585", size = 1) +
geom_area(fill = "#F72585", alpha = 0.2) +
labs(title = "F-Distribution (df1=5, df2=10)", x = "x", y = "f(x)") +
theme_minimal()
# ------------------------------------------------------------
# 3. Distribusi Chi-Square (df = 4)
# ------------------------------------------------------------
x3 <- seq(0, 15, length.out = 300)
df3 <- data.frame(x = x3, y = dchisq(x3, df = 4))
p3 <- ggplot(df3, aes(x, y)) +
geom_line(color = "#4CC9F0", size = 1) +
geom_area(fill = "#4CC9F0", alpha = 0.2) +
labs(title = "Chi-Square (df = 4)", x = "x", y = "f(x)") +
theme_minimal()
# ------------------------------------------------------------
# 4. Distribusi Gamma (shape = 2, rate = 1)
# ------------------------------------------------------------
x4 <- seq(0, 10, length.out = 300)
df4 <- data.frame(x = x4, y = dgamma(x4, shape = 2, rate = 1))
p4 <- ggplot(df4, aes(x, y)) +
geom_line(color = "#7209B7", size = 1) +
geom_area(fill = "#7209B7", alpha = 0.2) +
labs(title = "Gamma (shape=2, rate=1)", x = "x", y = "f(x)") +
theme_minimal()
# ------------------------------------------------------------
# 5. Distribusi Weibull (shape = 2, scale = 1)
# ------------------------------------------------------------
x5 <- seq(0, 3, length.out = 300)
df5 <- data.frame(x = x5, y = dweibull(x5, shape = 2, scale = 1))
p5 <- ggplot(df5, aes(x, y)) +
geom_line(color = "#3A0CA3", size = 1) +
geom_area(fill = "#3A0CA3", alpha = 0.2) +
labs(title = "Weibull (shape=2, scale=1)", x = "x", y = "f(x)") +
theme_minimal()
# ------------------------------------------------------------
# 6. Distribusi Eksponensial (rate = 1)
# ------------------------------------------------------------
x6 <- seq(0, 5, length.out = 300)
df6 <- data.frame(x = x6, y = dexp(x6, rate = 1))
p6 <- ggplot(df6, aes(x, y)) +
geom_line(color = "#4895EF", size = 1) +
geom_area(fill = "#4895EF", alpha = 0.2) +
labs(title = "Eksponensial (rate = 1)", x = "x", y = "f(x)") +
theme_minimal()
# ------------------------------------------------------------
# 7. Distribusi Pareto (xm = 1, alpha = 3)
# ------------------------------------------------------------
dpareto <- function(x, xm = 1, alpha = 3) {
ifelse(x >= xm, (alpha * (xm^alpha)) / (x^(alpha + 1)), 0)
}
x7 <- seq(1, 5, length.out = 300)
df7 <- data.frame(x = x7, y = dpareto(x7, xm = 1, alpha = 3))
p7 <- ggplot(df7, aes(x, y)) +
geom_line(color = "#10B981", size = 1) +
geom_area(fill = "#10B981", alpha = 0.2) +
labs(title = "Pareto (xm=1, alpha=3)", x = "x", y = "f(x)") +
theme_minimal()
# ------------------------------------------------------------
# 8. Distribusi Uniform Kontinu (min = 0, max = 5)
# ------------------------------------------------------------
x8 <- seq(-1, 6, length.out = 300)
df8 <- data.frame(x = x8, y = dunif(x8, min = 0, max = 5))
p8 <- ggplot(df8, aes(x, y)) +
geom_line(color = "#F59E0B", size = 1) +
geom_area(fill = "#F59E0B", alpha = 0.2) +
labs(title = "Uniform (min=0, max=5)", x = "x", y = "f(x)") +
theme_minimal()
# ------------------------------------------------------------
# Tampilkan Semua Plot dalam 1 Frame (2 Baris x 4 Kolom)
# ------------------------------------------------------------
grid.arrange(p1, p2, p3, p4, p5, p6, p7, p8, ncol = 4)

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)
)

library(ggplot2)
# 1. Tentukan rentang sumbu x yang memuat semua distribusi
# Karena ada Pareto dan Gamma yang tinggi, x dibatasi sampai 10 agar yang lain terlihat.
# Untuk x negatif (t-Student), kita buat sequence berbeda.
x_positive <- seq(0.01, 10, length.out = 1000)
x_negative <- seq(-4, -0.01, length.out = 300)
# 2. Definisikan rumus manual Pareto Tipe I (xm=1, alpha=3)
dpareto <- function(x, xm = 1, alpha = 3) {
ifelse(x >= xm, (alpha * (xm^alpha)) / (x^(alpha + 1)), 0)
}
# 3. Hitung density untuk tiap distribusi
# Gabungkan density x negatif (khusus t-student) dan x positif
df_list <- list(
# t-Student butuh x negatif dan positif
data.frame(x = c(x_negative, x_positive), y = dt(c(x_negative, x_positive), df = 5), Distribusi = "t-Student (df=5)"),
# Sisanya hanya x positif (karena x >= 0)
data.frame(x = x_positive, y = df(x_positive, df1 = 5, df2 = 10), Distribusi = "F (5, 10)"),
data.frame(x = x_positive, y = dchisq(x_positive, df = 4), Distribusi = "Chi-Square (df=4)"),
data.frame(x = x_positive, y = dgamma(x_positive, shape = 2, rate = 1), Distribusi = "Gamma (2,1)"),
data.frame(x = x_positive, y = dweibull(x_positive, shape = 2, scale = 1), Distribusi = "Weibull (2,1)"),
data.frame(x = x_positive, y = dexp(x_positive, rate = 1), Distribusi = "Eksponensial (rate=1)"),
data.frame(x = x_positive, y = dpareto(x_positive, xm = 1, alpha = 3), Distribusi = "Pareto (1,3)"),
data.frame(x = x_positive, y = dunif(x_positive, min = 0, max = 5), Distribusi = "Uniform (0, 5)")
)
df_long <- do.call(rbind, df_list)
# 4. Plot semua kurva dalam SATU grafik (Tanpa Facet)
ggplot(df_long, aes(x = x, y = y, color = Distribusi)) +
geom_line(size = 1.1) +
scale_y_continuous(limits = c(0, 1.2), expand = expansion(mult = c(0, 0.05))) + # Batasi y agar Pareto/Exp tidak terlalu dominan
labs(
title = "Perbandingan 8 Distribusi Kontinu",
subtitle = "Menampilkan semua garis kurva dalam satu plot tunggal",
x = "Nilai X",
y = "Kerapatan Probabilitas f(x)",
color = "Distribusi"
) +
scale_color_viridis_d(option = "plasma") + # Skema warna agar mudah dibedakan
theme_minimal() +
theme(
plot.title = element_text(face = "bold", size = 14),
legend.position = "right"
)

library(ggplot2)
# 1. Definisikan rentang nilai x
x_val <- seq(-4, 4, length.out = 1000)
# 2. Buat data frame gabungan untuk perbandingan df
df_plot <- rbind(
data.frame(x = x_val, y = dt(x_val, df = 1), Distribusi = "t (df = 1)"),
data.frame(x = x_val, y = dt(x_val, df = 5), Distribusi = "t (df = 5)"),
data.frame(x = x_val, y = dt(x_val, df = 30), Distribusi = "t (df = 30)"),
data.frame(x = x_val, y = dnorm(x_val), Distribusi = "Normal Standar Z")
)
# Kunci urutan legenda agar rapi
df_plot$Distribusi <- factor(df_plot$Distribusi,
levels = c("t (df = 1)", "t (df = 5)", "t (df = 30)", "Normal Standar Z"))
# 3. Plot Perbandingan Distribusi t dalam 1 Grafik
ggplot(df_plot, aes(x = x, y = y, color = Distribusi, linetype = Distribusi)) +
geom_line(size = 1.1) +
labs(
title = "Perbandingan Distribusi t-Student dan Normal Standar",
subtitle = "Makin besar derajat kebebasan (df), kurva t makin mendekati Normal Standar",
x = "Nilai t / Z",
y = "Kerapatan Probabilitas f(x)",
color = "Distribusi",
linetype = "Distribusi"
) +
scale_color_manual(values = c(
"t (df = 1)" = "#E63946",
"t (df = 5)" = "#457B9D",
"t (df = 30)" = "#2A9D8F",
"Normal Standar Z" = "black"
)) +
scale_linetype_manual(values = c(
"t (df = 1)" = "solid",
"t (df = 5)" = "solid",
"t (df = 30)" = "solid",
"Normal Standar Z" = "dashed"
)) +
theme_minimal() +
theme(
plot.title = element_text(face = "bold", size = 14),
plot.subtitle = element_text(size = 10, color = "gray30"),
legend.position = "right"
)
