Ở bài luyện tập này, mình sử dụng bộ dữ liệu kaggle_survey_2020_responses.csv được cung cấp bởi Kaggle năm 2020.
Từ hình ảnh, ta thấy khoảng chênh lệch giới tính trong ngành Data Science còn rất lớn. Mình đã lấy ra danh sách top 9 quốc gia có tỉ lệ lớn nhất và thêm Việt Nam để quan sát. Qua đó, có thể thấy rằng tỉ lệ nữ giới ở Việt Nam tham gia ngành này cũng ở mức triển vọng (14,3%), trong khi đó quốc gia lớn như Nhật Bản tỉ lệ này tương đối thấp (6,6%).
#Clear workspace:
rm(list = ls())
# Load package and data:
library(tidyverse)
df_raw <- read_csv("D:\\R\\kaggle_survey_2020_responses.csv")
# Rename for all columns:
df_raw %>%
slice(1) %>%
as.vector() -> df_a
str_replace_all(df_a, " ", "_") -> new_names
df_raw %>% slice(-1) -> df_raw
names(df_raw) <- new_names
# Select two columns:
df_raw %>%
select(c(3, 4)) %>%
rename(gender = `What_is_your_gender?_-_Selected_Choice`, nation = `In_which_country_do_you_currently_reside?`) ->
df_new
# Top 10 nations + Vietnam by nunber of DS/ML:
df_new %>%
group_by(nation) %>%
count() %>%
arrange(-n) %>%
ungroup() %>%
slice(1:9) %>%
pull(nation) -> top9_nations
c(top9_nations, "Viet Nam") -> top10
# Relabel for some countries:
df_new %>%
filter(nation %in% top10, gender %in% c("Man", "Woman")) %>%
mutate(nation = case_when(str_detect(nation, "United States") ~ "United States",
str_detect(nation, "Kingdom") ~ "United Kingdom",
TRUE ~ nation)) -> df_top10_nations
df_top10_nations %>%
group_by(gender, nation) %>%
count() %>%
ungroup() -> df_my
# Calculate female rate:
df_my %>%
spread(key = "gender", value = "n") %>%
mutate(rate = Woman / (Woman + Man)) %>%
arrange(rate) %>%
mutate(label = round(100*rate, 1)) %>%
mutate(label = as.character(label)) %>%
mutate(label = case_when(!str_detect(label, "\\.") ~ paste0(label, ".0"), TRUE ~ label)) %>%
mutate(label = paste0(label, "%")) %>%
mutate(nation = factor(nation, levels = nation)) -> df_rate
# Re-arrange order by female rate:
df_my %>%
mutate(nation = factor(nation, levels = df_rate$nation)) -> my_ord
# data visualization:
library(extrafont)
my_colors <- c("#2E74C0", "#CB454A")
my_font <- "Roboto Condensed"
# Graph fact 1:
my_ord %>%
full_join(df_rate, by = "nation") %>%
ggplot(aes(x = nation, y = n, fill = gender)) +
geom_col(position = "fill") +
coord_flip() +
geom_text(aes(x = nation, y = 0.04, label = label), size = 3.8, color = "white", family = my_font) +
scale_fill_manual(name = "", values = c(Man = my_colors[1], Woman = my_colors[2]), labels = c("Man", "Woman")) +
scale_y_continuous(labels = paste0(seq(0, 100, 25), "%"), expand = c(0, 0)) +
theme_minimal() +
theme(text = element_text(family = my_font)) +
theme(plot.margin = unit(rep(0.7, 4), "cm")) +
theme(plot.background = element_rect(fill = "#EFF2F4", color = NA)) +
theme(plot.title = element_text(size = 19)) +
theme(plot.caption = element_text(size = 10, color = "grey30")) +
theme(axis.text.x = element_text(size = 10.5, color = "grey20")) +
theme(axis.text.y = element_text(size = 10.5, color = "grey20")) +
theme(legend.position = "top") +
guides(fill = guide_legend(reverse = TRUE)) +
theme(panel.grid.major.y = element_blank(), panel.grid.minor.x = element_blank()) +
theme(legend.key.height = unit(0.15, "mm"), legend.key.width = unit(5, "mm")) +
labs(x = NULL, y = NULL,
title = "Fact 1: Women in Machine Learning and Data Science Community",
caption = "Source: 2020 Kaggle ML & DS Survey")Phần tiếp theo, mình thực hiện minh họa các nhóm tuổi phân loại theo giới tính của tất cả các nước trên thế giới. Kết quả cho thấy rằng, nhóm Millennials (Gen Y) hiện đang thống trị ngành Data Science, trong đó độ tuổi phổ biến nhất từ 25-29 tuổi. Tuy nhiên, tỉ lệ cũng cho thấy nhóm Gen Z đang quan tâm rất lớn vào ngành này. Trong tương lai, Gen Z chắc chắn sẽ làm khuấy động ngành ML & DS.
df_raw %>%
group_by(`What_is_your_age_(#_years)?`, `What_is_your_gender?_-_Selected_Choice`) %>%
count() %>%
ungroup() -> age_gender
names(age_gender) <- c("Age", "Gender", "n")
age_gender %>%
filter(Gender %in% c("Man", "Woman")) -> age_gender
age_gender$Age %>% unique() -> age
age_gender %>%
mutate(Age = factor(Age, levels = age)) %>%
mutate(n = as.numeric(n)) %>%
mutate(c = case_when(Gender == "Man" ~ -1*n, TRUE ~ n)) -> age_gender
age_gender %>%
ggplot(aes(x = Age, y = c, fill = Gender)) +
geom_col() +
coord_flip() +
theme_minimal() +
theme(text = element_text(family = my_font)) +
theme(plot.margin = unit(rep(0.7, 4), "cm")) +
theme(plot.title = element_text(size = 19)) +
theme(plot.caption = element_text(size = 10, color = "grey30")) +
theme(plot.background = element_rect(fill = "#EFF2F4", color = NA)) +
theme(axis.text.x = element_text(size = 10.5, color = "grey20")) +
theme(axis.text.y = element_text(size = 10.5, color = "grey20")) +
theme(legend.position = "top") +
scale_fill_manual(name = "", values = c(Man = my_colors[1], Woman = my_colors[2]), labels = c("Man", "Woman")) +
guides(fill = guide_legend(reverse = TRUE)) +
theme(panel.grid.major.y = element_blank()) +
theme(panel.grid.major.x = element_blank()) +
theme(legend.key.height = unit(0.15, "mm"), legend.key.width = unit(5, "mm")) +
scale_y_continuous(breaks = seq(-4000, 1000, 500), labels = c(seq(4000, 500, -500), seq(0, 1000, 500))) +
theme(panel.grid.major.x = element_line(color = "grey60", linetype = "dotted")) +
labs(x = NULL, y = NULL,
title = "Fact 2: Age Distribution by Gender",
caption = "Source: 2020 Kaggle ML & DS Survey")