employed <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-02-23/employed.csv")
## Rows: 8184 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (4): industry, major_occupation, minor_occupation, race_gender
## dbl (3): industry_total, employ_n, year
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
1: Convert Data to Standardized Form
employed_group <- employed %>%
filter(!is.na(employ_n)) %>%
group_by(occupation = paste(industry, minor_occupation), race_gender) %>%
summarise(n = sum(employ_n)) %>%
ungroup()
## `summarise()` has grouped output by 'occupation'. You can override using the
## `.groups` argument.
employed_tidy <- employed_group %>%
# Removing TOTAL category in race_gender column
filter(race_gender != "TOTAL") %>%
# Adding the TOTAL column
left_join(employed_group %>%
filter(race_gender == "TOTAL") %>%
select(occupation, total = n)) %>%
# Get % in total column
mutate(pct = n / total) %>%
# Find outliers by employed %>% filter(race_gender == "TOTAL") %>% pull(employ_n) %>% summary() in console
# Remove outliers
filter(total > 1000) %>%
select(-n)
## Joining with `by = join_by(occupation)`
employed_tidy
## # A tibble: 1,160 × 4
## occupation race_gender total pct
## <chr> <chr> <dbl> <dbl>
## 1 Agriculture and related Construction and extractio… Asian 7.3 e4 0.0274
## 2 Agriculture and related Construction and extractio… Black or A… 7.3 e4 0.0822
## 3 Agriculture and related Construction and extractio… Men 7.3 e4 0.973
## 4 Agriculture and related Construction and extractio… White 7.3 e4 0.863
## 5 Agriculture and related Construction and extractio… Women 7.3 e4 0.0274
## 6 Agriculture and related Farming, fishing, and fore… Asian 5.74e6 0.0139
## 7 Agriculture and related Farming, fishing, and fore… Black or A… 5.74e6 0.0342
## 8 Agriculture and related Farming, fishing, and fore… Men 5.74e6 0.789
## 9 Agriculture and related Farming, fishing, and fore… White 5.74e6 0.911
## 10 Agriculture and related Farming, fishing, and fore… Women 5.74e6 0.211
## # ℹ 1,150 more rows
employed_standard <- employed_tidy %>%
# Standardize
group_by(race_gender) %>%
mutate(pct = pct %>% scale() %>% as.numeric()) %>%
ungroup() %>%
mutate(total = total %>% log() %>% scale() %>% as.numeric())
employed_standard
## # A tibble: 1,160 × 4
## occupation race_gender total pct
## <chr> <chr> <dbl> <dbl>
## 1 Agriculture and related Construction and extractio… Asian -1.30 -0.539
## 2 Agriculture and related Construction and extractio… Black or A… -1.30 -0.405
## 3 Agriculture and related Construction and extractio… Men -1.30 1.31
## 4 Agriculture and related Construction and extractio… White -1.30 0.725
## 5 Agriculture and related Construction and extractio… Women -1.30 -1.30
## 6 Agriculture and related Farming, fishing, and fore… Asian 0.819 -0.928
## 7 Agriculture and related Farming, fishing, and fore… Black or A… 0.819 -1.21
## 8 Agriculture and related Farming, fishing, and fore… Men 0.819 0.510
## 9 Agriculture and related Farming, fishing, and fore… White 0.819 1.38
## 10 Agriculture and related Farming, fishing, and fore… Women 0.819 -0.503
## # ℹ 1,150 more rows
4: Selecting Optimal Number of Clusters
kclust <- tibble(k = 1:9) %>%
mutate(kclust = map(.x = k, .f = ~kmeans(occupation_demo_tbl %>% select(-occupation), centers = .x, nstart = 20)),
glanced = map(.x = kclust, .f = glance))
kclust %>%
unnest(glanced) %>%
ggplot(aes(k, tot.withinss)) +
geom_point() +
geom_line()

final_cluster <- kmeans(occupation_demo_tbl %>% select(-occupation), centers = 5, nstart = 20)
augment(final_cluster, occupation_demo_tbl) %>%
ggplot(aes(total, asian, color = .cluster)) +
geom_point()

5: Reduce dimension using UMAP
umap_results <- occupation_demo_tbl %>%
select(-occupation) %>%
umap()
umap_results_tbl <- umap_results$layout %>%
as.tibble() %>%
bind_cols(occupation_demo_tbl %>% select(occupation))
## Warning: `as.tibble()` was deprecated in tibble 2.0.0.
## ℹ Please use `as_tibble()` instead.
## ℹ The signature and semantics have changed, see `?as_tibble`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The `x` argument of `as_tibble.matrix()` must have unique column names if
## `.name_repair` is omitted as of tibble 2.0.0.
## ℹ Using compatibility `.name_repair`.
## ℹ The deprecated feature was likely used in the tibble package.
## Please report the issue at <https://github.com/tidyverse/tibble/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
umap_results_tbl
## # A tibble: 232 × 3
## V1 V2 occupation
## <dbl> <dbl> <chr>
## 1 -7.60 -6.38 Agriculture and related Construction and extraction occupatio…
## 2 -0.946 8.20 Agriculture and related Farming, fishing, and forestry occupa…
## 3 -2.61 -5.74 Agriculture and related Installation, maintenance, and repair…
## 4 10.9 -0.951 Agriculture and related Manage-ment, business, and financial …
## 5 -0.0991 8.01 Agriculture and related Management, business, and financial o…
## 6 2.50 -6.31 Agriculture and related Office and administrative support occ…
## 7 -2.24 -5.84 Agriculture and related Production occupations
## 8 -0.267 -5.92 Agriculture and related Professional and related occupations
## 9 -7.45 -6.30 Agriculture and related Protective service occupations
## 10 -7.22 -6.14 Agriculture and related Sales and related occupations
## # ℹ 222 more rows
umap_results_tbl %>%
ggplot(aes(V1, V2, text = occupation)) +
geom_point()

6: Visualizing clusters by adding k-means results
kmeans_umap_tbl <- final_cluster %>%
augment(occupation_demo_tbl) %>%
select(occupation, .cluster) %>%
# Add UMAP results
left_join(umap_results_tbl) %>%
# Add employment info
left_join(employed_tidy %>%
select(-total) %>%
pivot_wider(names_from = race_gender, values_from = pct) %>%
janitor::clean_names())
## Joining with `by = join_by(occupation)`
## Joining with `by = join_by(occupation)`
kmeans_umap_tbl
## # A tibble: 232 × 9
## occupation .cluster V1 V2 asian black_or_african_ame…¹ men white
## <chr> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Agricultu… 4 -7.60 -6.38 0.0274 0.0822 0.973 0.863
## 2 Agricultu… 3 -0.946 8.20 0.0139 0.0342 0.789 0.911
## 3 Agricultu… 4 -2.61 -5.74 0.0155 0.0309 0.985 0.918
## 4 Agricultu… 4 10.9 -0.951 0.00992 0.00794 0.739 0.967
## 5 Agricultu… 3 -0.0991 8.01 0.00997 0.00882 0.741 0.962
## 6 Agricultu… 4 2.50 -6.31 0.0233 0.0155 0.159 0.938
## 7 Agricultu… 4 -2.24 -5.84 0.0332 0.104 0.815 0.820
## 8 Agricultu… 4 -0.267 -5.92 0.0339 0.0373 0.675 0.902
## 9 Agricultu… 4 -7.45 -6.30 0 0.0682 0.864 0.875
## 10 Agricultu… 4 -7.22 -6.14 0 0.0213 0.585 0.968
## # ℹ 222 more rows
## # ℹ abbreviated name: ¹black_or_african_american
## # ℹ 1 more variable: women <dbl>
g <- kmeans_umap_tbl %>%
# Create text label
mutate(text_label = str_glue("Occupation: {occupation}
Cluster: {.cluster}
Asian: {asian %>% scales::percent(1)}
Black: {black_or_african_american %>% scales::percent(1)}
Men: {men %>% scales::percent(1)}
White: {white %>% scales::percent(1)}
Women: {women %>% scales::percent(1)}")) %>%
# Plot
ggplot(aes(V1, V2, color = .cluster, text = text_label)) +
geom_point()
# Adding labels to points
g %>% ggplotly(tooltip = "text")