data <- read.csv("C:\\Users\\ade\\Downloads\\ds_salaries.csv", sep= ",")
str (data)
## 'data.frame':    607 obs. of  12 variables:
##  $ id                : int  0 1 2 3 4 5 6 7 8 9 ...
##  $ work_year         : int  2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 ...
##  $ experience_level  : chr  "MI" "SE" "SE" "MI" ...
##  $ employment_type   : chr  "FT" "FT" "FT" "FT" ...
##  $ job_title         : chr  "Data Scientist" "Machine Learning Scientist" "Big Data Engineer" "Product Data Analyst" ...
##  $ salary            : int  70000 260000 85000 20000 150000 72000 190000 11000000 135000 125000 ...
##  $ salary_currency   : chr  "EUR" "USD" "GBP" "USD" ...
##  $ salary_in_usd     : int  79833 260000 109024 20000 150000 72000 190000 35735 135000 125000 ...
##  $ employee_residence: chr  "DE" "JP" "GB" "HN" ...
##  $ remote_ratio      : int  0 0 50 0 50 100 100 50 100 50 ...
##  $ company_location  : chr  "DE" "JP" "GB" "HN" ...
##  $ company_size      : chr  "L" "S" "M" "S" ...
  1. Visualisasi Nilai
rataan_gaji <- aggregate(salary_in_usd ~ job_title, data=data, FUN=mean)
rataan_gaji <- rataan_gaji %>%
  arrange(desc(salary_in_usd))
View(rataan_gaji)
top_10_gaji <- rataan_gaji %>%
  slice_head(n = 10)
ggplot(data= top_10_gaji,
       aes(x= salary_in_usd, y= reorder(job_title, salary_in_usd), fill= salary_in_usd)) +
  geom_bar(stat= "identity") +
  geom_text(aes(label= salary_in_usd), hjust= 1.1, color= "white", size= 3) +
  labs(title= "Top 10 Job Title dengan Gaji Tertinggi",
       caption= "Sumber: Badan Pusat Statistik", y= "Job title", x= "Gaji (US Dollar)") +
  scale_fill_gradient(low= "orange", high= "red") +
   theme(plot.title = element_text(hjust = 0.5))

data_remote <- table(data$remote_ratio)
data_remote <- data.frame(value= names(data_remote), frequency= as.numeric(data_remote))
ggplot(data_remote, aes(x = value, y = frequency, label = frequency)) +
  geom_bar(stat = "identity", fill = "skyblue", color = "black") +
  geom_text(size = 3, position = position_stack(vjust = 0.5)) +
  labs(x = "Remote Ratio", y = "Frequency", title = "Bar Chart of Remote Ratio") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5))

# Membuat data hanya job dan merubah dalam tipe numeric
top10_job <- table(data$job_title)
top10_job <- data.frame(value= names(top10_job), frequency= as.numeric(top10_job)) 

# Mengurutkan job dari yang terbesar ke yang terkecil
top10_job <- top10_job %>%
  arrange(desc(frequency))

# Menyusun data frame untuk top 10 job
top10_job <- data %>%
  group_by(job_title) %>%
  summarise(frequency = n()) %>%
  arrange(desc(frequency)) %>%
  slice(1:10)

# Membuat plot
ggplot(data= top10_job,
       aes(x= frequency, y= reorder(job_title, frequency), fill= frequency)) +
  geom_bar(stat= "identity") +
  geom_text(aes(label= frequency), hjust= 1.5, color= "white", size= 3) +
  labs(title= "Top 10 Job Terbanyak",
       caption= "Sumber: Badan Pusat Statistik", y= "Job title", x= "frequency") +
  scale_fill_gradient(low= "lightblue", high= "blue") +
   theme(plot.title = element_text(hjust = 0.5))

2. Visualisasi Sebaran

ggplot(data = data) +
  geom_density(aes(x = salary_in_usd, fill = as.factor(work_year), color = as.factor(work_year)), alpha = 0.5) +
  labs(title = "Density Plot Sebaran Data Gaji Tiap Tahun",
       x = "Gaji (US Dollar)",
       y = "Jumlah") +
  theme(plot.title = element_text(hjust = 0.5))

data$experience_level <- factor(data$experience_level, levels = c("EN", "MI", "SE", "EX"))
ggplot(data, aes(experience_level, salary_in_usd)) + 
  geom_violin(aes(col = experience_level), fill = NA, draw_quantiles = c(0.25,0.5,0.75))  +
  labs(title = "Violin Plot Sebaran Data Gaji Berdasarkan Pengalaman Kerja",
       caption = "Sumber: Badan Pusat Statistik")

# Mengubah variabel Bulan menjadi faktor dengan urutan yang diinginkan
data$experience_level <- factor(data$experience_level, levels = c("EN", "MI", "SE", "EX"))

ggplot(data = data, aes(x = experience_level, y = salary_in_usd, fill = experience_level , color=experience_level)) +
  geom_boxplot(color="black") +
  theme()+
  scale_fill_manual(values = c("#711DB0", "#C21292", "#EF4040", "#FFA732")) +
  labs(x = NULL, y = "Besar Gaji (US Dollar")

  1. Visualisasi Proporsi
#Membuat data baru job title berdasarkan frekuensi job title 
ds_job <- table(data$job_title)
ds_job <- data.frame(value = (names(ds_job)), frequency = as.numeric(ds_job))
treemap(ds_job,
        index = "value",  # Kolom untuk kategori
        vSize = "frequency",     # Kolom untuk ukuran
        title = "Proporsi Job title")  # Judul treemap

#Subset data
exp.S <- subset(data[,c("company_size","experience_level")], company_size == "S")
exp.S <- exp.S[,"experience_level"] 

exp.M <- subset(data[,c("company_size","experience_level")], company_size == "M")
exp.M <- exp.M[,"experience_level"] 

exp.L <- subset(data[,c("company_size","experience_level")], company_size == "L")
exp.L <- exp.L[,"experience_level"] 

category.exp.S <- table(exp.S)
category.exp.M <- table(exp.M)
category.exp.L <- table(exp.L)

dat.exp.S <- data.frame(value = (names(category.exp.S)), frequency = as.numeric(category.exp.S))
dat.exp.M <- data.frame(value = (names(category.exp.M)), frequency = as.numeric(category.exp.M))
dat.exp.L <- data.frame(value = (names(category.exp.L)), frequency = as.numeric(category.exp.L))

data.exp <- data.frame(dat.exp.S, dat.exp.M[,"frequency"], dat.exp.L[,"frequency"])
colnames(data.exp) <- c("experience_level", "S", "M", "L")

# Melt the data
data.exp.melt <- melt(data.exp, id.vars = "experience_level")

# Calculate proportions
data.exp.melt <- ddply(data.exp.melt, "experience_level", transform, prop = value / sum(value))

# Plot
ggplot(data.exp.melt, aes(x = experience_level, y = prop, fill = variable)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = scales::percent(prop), group = variable), 
            position = position_stack(vjust = 0.5), 
            color = "white", size = 3) +  # Menambahkan label proporsi
  scale_y_continuous(labels = scales::percent_format()) +
  labs(x = "experience_level", y = "Proportion", fill = "Group") +
  ggtitle("Stacked Bar Chart of Proportions") +
  theme_minimal()+
  theme(plot.margin = margin(0, -5, 0, 0, "pt"))+
  coord_flip()