# Load package
library(tidyverse)       # Analisis data
library(scales)          # Pemformatan skala
library(fivethirtyeight) # Dataset FiveThirtyEight

# Load dataset utama
data("college_recent_grads")

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

# Menampilkan struktur dataset
glimpse(college_recent_grads)
## Rows: 173
## Columns: 21
## $ rank                        <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,…
## $ major_code                  <int> 2419, 2416, 2415, 2417, 2405, 2418, 6202, …
## $ major                       <chr> "Petroleum Engineering", "Mining And Miner…
## $ major_category              <chr> "Engineering", "Engineering", "Engineering…
## $ total                       <int> 2339, 756, 856, 1258, 32260, 2573, 3777, 1…
## $ sample_size                 <int> 36, 7, 3, 16, 289, 17, 51, 10, 1029, 631, …
## $ men                         <int> 2057, 679, 725, 1123, 21239, 2200, 2110, 8…
## $ women                       <int> 282, 77, 131, 135, 11021, 373, 1667, 960, …
## $ sharewomen                  <dbl> 0.1205643, 0.1018519, 0.1530374, 0.1073132…
## $ employed                    <int> 1976, 640, 648, 758, 25694, 1857, 2912, 15…
## $ employed_fulltime           <int> 1849, 556, 558, 1069, 23170, 2038, 2924, 1…
## $ employed_parttime           <int> 270, 170, 133, 150, 5180, 264, 296, 553, 1…
## $ employed_fulltime_yearround <int> 1207, 388, 340, 692, 16697, 1449, 2482, 82…
## $ unemployed                  <int> 37, 85, 16, 40, 1672, 400, 308, 33, 4650, …
## $ unemployment_rate           <dbl> 0.018380527, 0.117241379, 0.024096386, 0.0…
## $ p25th                       <dbl> 95000, 55000, 50000, 43000, 50000, 50000, …
## $ median                      <dbl> 110000, 75000, 73000, 70000, 65000, 65000,…
## $ p75th                       <dbl> 125000, 90000, 105000, 80000, 75000, 10200…
## $ college_jobs                <int> 1534, 350, 456, 529, 18314, 1142, 1768, 97…
## $ non_college_jobs            <int> 364, 257, 176, 102, 4440, 657, 314, 500, 1…
## $ low_wage_jobs               <int> 193, 50, 0, 0, 972, 244, 259, 220, 3253, 3…
# Melihat beberapa baris pertama
head(college_recent_grads)
## # A tibble: 6 × 21
##    rank major_code major major_category total sample_size   men women sharewomen
##   <int>      <int> <chr> <chr>          <int>       <int> <int> <int>      <dbl>
## 1     1       2419 Petr… Engineering     2339          36  2057   282      0.121
## 2     2       2416 Mini… Engineering      756           7   679    77      0.102
## 3     3       2415 Meta… Engineering      856           3   725   131      0.153
## 4     4       2417 Nava… Engineering     1258          16  1123   135      0.107
## 5     5       2405 Chem… Engineering    32260         289 21239 11021      0.342
## 6     6       2418 Nucl… Engineering     2573          17  2200   373      0.145
## # ℹ 12 more variables: employed <int>, employed_fulltime <int>,
## #   employed_parttime <int>, employed_fulltime_yearround <int>,
## #   unemployed <int>, unemployment_rate <dbl>, p25th <dbl>, median <dbl>,
## #   p75th <dbl>, college_jobs <int>, non_college_jobs <int>,
## #   low_wage_jobs <int>

Including Plots

You can also embed plots, for example:

# Menambahkan kategori STEM dan Non-STEM
college_recent_grads <- college_recent_grads %>%
  mutate(stem = ifelse(major_category %in% c("Engineering", "Physical Sciences", 
                                             "Biology & Life Science", "Math & Computer Science"), 
                       "STEM", "Non-STEM"))

# Plot gaji berdasarkan kategori
ggplot(college_recent_grads, aes(x = stem, y = median, fill = stem)) +
  geom_boxplot() +
  scale_y_continuous(labels = scales::dollar_format()) +
  labs(title = "Perbandingan Median Salary: STEM vs. Non-STEM",
       x = "Kategori Jurusan", y = "Median Salary") +
  theme_minimal()

Exercise 1

Ada tiga jenis pendapatan yang dilaporkan dalam data frame ini: p25th, median, dan p75th. Ketiganya masing-masing merujuk pada persentil ke-25, ke-50, dan ke-75 dari distribusi pendapatan individu yang diambil sampelnya untuk suatu jurusan tertentu. Mengapa kita sering memilih median daripada mean untuk menggambarkan pendapatan tipikal suatu kelompok?

# Load library yang dibutuhkan
library(tidyverse)
library(fivethirtyeight)

# Load dataset
data("college_recent_grads", package = "fivethirtyeight")

# Hitung mean dan median pendapatan
summary_stats <- college_recent_grads %>%
  summarise(
    mean_income = mean(median, na.rm = TRUE),
    median_income = median(median, na.rm = TRUE)
  )

# Tampilkan hasil
print(summary_stats)
## # A tibble: 1 × 2
##   mean_income median_income
##         <dbl>         <dbl>
## 1      40151.         36000
# Plot histogram dengan warna dan label tambahan
ggplot(college_recent_grads, aes(x = median)) +  
  geom_histogram(binwidth = 5000, fill = "orange", alpha = 0.7, color = "black") + 
  geom_vline(xintercept = summary_stats$mean_income, color = "blue", linetype = "dashed", size = 1.2) +  
  geom_vline(xintercept = summary_stats$median_income, color = "purple", linetype = "dashed", size = 1.2) +
  geom_text(aes(x = summary_stats$mean_income, y = 30, label = "Mean"), color = "blue", angle = 90, vjust = -0.5) +
  geom_text(aes(x = summary_stats$median_income, y = 30, label = "Median"), color = "purple", angle = 90, vjust = -0.5) +
  labs(title = "Distribusi Pendapatan Median Lulusan Berdasarkan Jurusan",
       subtitle = "Garis biru: mean | Garis ungu: median",
       x = "Pendapatan Median ($)", y = "Jumlah Jurusan") +
  theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning in geom_text(aes(x = summary_stats$mean_income, y = 30, label = "Mean"), : All aesthetics have length 1, but the data has 173 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
##   a single row.
## Warning in geom_text(aes(x = summary_stats$median_income, y = 30, label = "Median"), : All aesthetics have length 1, but the data has 173 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
##   a single row.

## Including Plots

Buat ulang visualisasi berikut. Catatan: Lebar bin yang digunakan adalah $5.000. Perhatikan dengan cermat teks dan label pada sumbu.

Hint: The label_dollar() function can be helpful for the x-axis.

Hint: Wondering how to restrict to full-time, year-round workers? Have a look at the data dictionary by typing ?college_recent_grads into the console. It’s probably easier than you think!

# Load library
library(tidyverse)
library(fivethirtyeight)
library(scales)

# Load dataset
data("college_recent_grads", package = "fivethirtyeight")

# Filter hanya untuk kategori STEM
stem_majors <- college_recent_grads %>%
  filter(major_category %in% c("Biology & Life Science", 
                               "Computers & Mathematics", 
                               "Engineering", 
                               "Physical Sciences"))

# Plot histogram dengan warna berbeda untuk setiap kategori
ggplot(stem_majors, aes(x = median, fill = major_category)) +
  geom_histogram(binwidth = 5000, alpha = 0.8, color = "black") +  # Warna mengikuti kategori
  scale_x_continuous(labels = label_dollar()) + 
  facet_wrap(~ major_category, scales = "free_y") + 
  labs(
    title = "Distribusi Pendapatan Median Lulusan STEM",
    subtitle = "Analisis berdasarkan kategori jurusan",
    x = "Median earnings",
    y = "Frequency"
  ) +
  theme_minimal() +
  theme(
    legend.position = "none",
    panel.grid.major = element_line(color = "gray85")
  )

Including Plots

Buat ulang visualisasi dari latihan sebelumnya, kali ini dengan lebar bin sebesar $1.000. Mana yang lebih baik antara $1.000 atau $5.000 sebagai pilihan lebar bin? Jelaskan alasan Anda dalam satu kalimat.

# Load library
library(tidyverse)
library(fivethirtyeight)
library(scales)

# Load dataset
data("college_recent_grads", package = "fivethirtyeight")

# Filter hanya untuk STEM majors
stem_majors <- college_recent_grads %>%
  filter(major_category %in% c("Biology & Life Science", 
                               "Computers & Mathematics", 
                               "Engineering", 
                               "Physical Sciences"))

# Plot histogram dengan binwidth 1000
ggplot(stem_majors, aes(x = median, fill = major_category)) +
  geom_histogram(binwidth = 1000, alpha = 0.7, color = "black") +  # Perbaikan di sini
  scale_x_continuous(labels = scales::label_dollar()) + 
  facet_wrap(~ major_category, scales = "free_y") + 
  labs(
    title = "Median earnings of full-time, year-round workers",
    subtitle = "For STEM majors (Binwidth: $1,000)",
    x = "Median earnings",
    y = "Frequency"
  ) +
  theme_minimal() +
  theme(legend.position = "none")

Including Plots

Jurusan STEM mana (yaitu, jurusan dalam kategori “Biology & Life Science”, “Computers & Mathematics”, “Engineering”, dan “Physical Sciences”) yang memiliki gaji median yang sama dengan atau lebih rendah dari median untuk seluruh jurusan (semua jurusan, bukan hanya yang termasuk dalam kategori STEM)? Output Anda hanya boleh menampilkan nama jurusan serta pendapatan median, persentil ke-25, dan persentil ke-75 untuk jurusan tersebut, dan harus diurutkan sehingga jurusan dengan pendapatan median tertinggi berada di bagian atas.

# Load library
library(tidyverse)
library(fivethirtyeight)

# Load dataset
data("college_recent_grads", package = "fivethirtyeight")

# Filter jurusan STEM
stem_majors <- college_recent_grads %>%
  filter(major_category %in% c("Biology & Life Science", 
                               "Computers & Mathematics", 
                               "Engineering", 
                               "Physical Sciences"))

# Cari nilai median dari seluruh jurusan (bukan hanya STEM)
overall_median <- median(college_recent_grads$median, na.rm = TRUE)

# Filter jurusan STEM yang memiliki median salary ≤ overall median
filtered_stem <- stem_majors %>%
  filter(median <= overall_median) %>%
  select(major, median, p25th, p75th) %>%
  arrange(desc(median))  # Urutkan berdasarkan median salary tertinggi

ggplot(filtered_stem, aes(x = reorder(major, median), y = median, fill = median)) +
  geom_col(show.legend = FALSE) +
  coord_flip() +
  scale_y_continuous(labels = scales::label_dollar()) +
  labs(
    title = "STEM Majors with Median Earnings Below Overall Median",
    x = "Major",
    y = "Median Earnings ($)"
  ) +
  theme_minimal()

# Tampilkan hasil
print(filtered_stem)
## # A tibble: 11 × 4
##    major                                 median p25th p75th
##    <chr>                                  <dbl> <dbl> <dbl>
##  1 Geosciences                            36000 21000 41000
##  2 Environmental Science                  35600 25000 40200
##  3 Multi-Disciplinary Or General Science  35000 24000 50000
##  4 Physiology                             35000 20000 50000
##  5 Communication Technologies             35000 25000 45000
##  6 Neuroscience                           35000 30000 44000
##  7 Atmospheric Sciences And Meteorology   35000 28000 50000
##  8 Miscellaneous Biology                  33500 23000 48000
##  9 Biology                                33400 24000 45000
## 10 Ecology                                33000 23000 42000
## 11 Zoology                                26000 20000 39000

Including Plots

You can also embed plots, for example:

## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 1 row containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_point()`).