# Data dan library
data_ongkir <- data.frame(
  Produk = rep(c("Elektronik", "Pakaian", "Kosmetik", "Buku", "Makanan"), each = 2),
  Kota = rep(c("A", "B"), times = 5),
  Ongkir = c(28, 25, 20, 22, 5, 18, 16, 17, 14, 13)
)

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.4.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(webr)
## Warning: package 'webr' was built under R version 4.4.3
ongkir_kota <- data_ongkir %>%
  group_by(Kota) %>%
  summarise(TotalOngkir = sum(Ongkir), .groups = "drop")

ggplot(ongkir_kota, aes(x = "", y = TotalOngkir, fill = Kota)) +
  geom_bar(width = 1, stat = "identity") +
  coord_polar("y", start = 0) +
  labs(title = "Distribusi Ongkir per Kota") +
  theme_void() +
  geom_text(aes(label = scales::percent(TotalOngkir / sum(TotalOngkir), accuracy = 1)),
            position = position_stack(vjust = 0.5)) +
  scale_fill_manual(values = c("skyblue", "salmon"))

df2 <- data_ongkir %>%
  group_by(Kota) %>%
  summarise(frekuensi = sum(Ongkir)) %>%
  mutate(label = paste0(round(frekuensi / sum(frekuensi) * 100), "%"))

ggplot(df2, aes(x = 1.5, y = frekuensi, fill = Kota)) +
  geom_bar(stat = "identity", width = 1, color = "white") +
  coord_polar(theta = "y") +
  xlim(0.5, 2.5) +
  theme_void() +
  scale_fill_manual(values = c("A" = "blue", "B" = "red")) +
  geom_text(aes(label = label), position = position_stack(vjust = 0.5), color = "black", size = 5) +
  labs(title = "Distribusi Ongkir per Kota")

df3 <- data_ongkir %>%
  group_by(Kota, Produk) %>%
  summarise(Freq = sum(Ongkir), .groups = "drop")

PieDonut(df3, 
         aes(Kota, Produk, count = Freq), 
         r0 = 0.5, r1 = 1, r2 = 1.3,
         title = "Distribusi Ongkir per Produk dan Kota",
         labelposition = 0.2)
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.
## ℹ The deprecated feature was likely used in the webr package.
##   Please report the issue at <https://github.com/cardiomoon/webr/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

ggplot(data_ongkir, aes(y = Ongkir)) + 
  geom_boxplot(fill = "#00FFFF") +
  labs(title = "Boxplot Ongkir", y = "Ongkir") +
  theme_minimal()

ggplot(data_ongkir, aes(x = Produk, y = Ongkir)) + 
  geom_boxplot(fill = "violet") +
  labs(title = "Boxplot Ongkir per Produk", x = "Produk", y = "Ongkir") +
  theme_minimal()

ggplot(data_ongkir, aes(x = Ongkir)) + 
  geom_histogram(bins = 5, fill = "lightblue", color = "black") +
  labs(title = "Histogram Ongkir", x = "Ongkir", y = "Frekuensi") +
  theme_minimal()

ggplot(data_ongkir, aes(x = Ongkir, fill = Kota)) +
  geom_density(alpha = 0.4) +
  labs(title = "Density Plot Ongkir per Kota", x = "Ongkir") +
  theme_classic()