/day3/equality-line.R

https://github.com/ikashnitsky/dataviz-mpidr · R · 74 lines · 51 code · 14 blank · 9 comment · 1 complexity · ead82fd2dd44e9528f30ad4cf0dc02f4 MD5 · raw file

  1. #===============================================================================
  2. # 2021-06-16 -- MPIDR dataviz
  3. # Equality line plot
  4. # Ilya Kashnitsky, ilya.kashnitsky@gmail.com
  5. #===============================================================================
  6. # links --------------------------------------------------------------
  7. # https://twitter.com/cchappas/status/1076910768711446528
  8. # play with GGP educational attainment data -------------------------------
  9. library(tidyverse)
  10. educ <- read_csv2("data/CDB_EAUS.csv", na = "..", skip = 2) %>%
  11. pivot_longer(3:50, names_to = "year") %>%
  12. mutate(value = value / 100) %>%
  13. pivot_wider(names_from = Sex, values_from = value) %>%
  14. clean_names() %>%
  15. separate(country_engl_name_nat_name, into = c("cntr", "rest"), sep = "/") %>%
  16. transmute(
  17. id = cntr %>% str_sub(1,2),
  18. name = cntr %>% str_sub(3, -1),
  19. year,
  20. total, female, male
  21. ) %>%
  22. drop_na()
  23. library(ggdark)
  24. library(ggrepel)
  25. educ %>%
  26. ggplot(aes(female, male, group = name, color = year))+
  27. geom_abline(slope = 1)+
  28. geom_path()+
  29. geom_point(size = .3)+
  30. scale_color_viridis_d(option = "B", begin = .2, end = .9)+
  31. coord_fixed(xlim = c(0, 100), ylim = c(0, 100), expand = F)+
  32. geom_text_repel(
  33. data = . %>%
  34. drop_na(total) %>%
  35. group_by(name) %>%
  36. arrange(year %>% desc) %>%
  37. slice(1) %>%
  38. dplyr::ungroup(),
  39. aes(label = name), size = 2, color = "white",
  40. nudge_x = 2, nudge_y = 2
  41. )+
  42. dark_theme_minimal()
  43. # one year
  44. educ %>%
  45. filter(year == "2015") %>%
  46. ggplot(aes(female, male, group = name, color = total))+
  47. geom_abline(slope = 1)+
  48. geom_point(size = 2)+
  49. scale_color_viridis_c(option = "B", begin = .2, end = .9)+
  50. coord_fixed(xlim = c(0, 100), ylim = c(0, 100), expand = F)+
  51. geom_text_repel(
  52. data = . %>%
  53. drop_na(total) %>%
  54. group_by(name) %>%
  55. slice(1) %>%
  56. dplyr::ungroup(),
  57. aes(label = name), size = 2, color = "white"
  58. )+
  59. dark_theme_minimal()+
  60. theme(legend.position = "none")