/tidytuesday_201939_school_diversity.r

https://github.com/spren9er/tidytuesday · R · 60 lines · 38 code · 13 blank · 9 comment · 0 complexity · ecd3de820f03154130dd3be20b00af17 MD5 · raw file

  1. library(tidyverse)
  2. library(janitor)
  3. path <-
  4. paste0(
  5. 'https://raw.githubusercontent.com/rfordatascience/tidytuesday/',
  6. 'master/data/2019/2019-09-24/'
  7. )
  8. data <- read_csv(paste0(path, 'school_diversity.csv')) %>%
  9. clean_names()
  10. data <- data %>%
  11. select(leaid, total, school_year, diverse) %>%
  12. rename(id = leaid) %>%
  13. mutate(
  14. school_year = str_sub(school_year, 1, 4),
  15. diverse = fct_recode(
  16. diverse,
  17. '1' = 'Diverse', '0' = 'Undiverse', '-1' = 'Extremely undiverse'
  18. )
  19. ) %>%
  20. pivot_wider(names_from = school_year, values_from = c(diverse, total)) %>%
  21. drop_na()
  22. nrow(data)
  23. # raw data -> d3 (use d3.force layout to calculate source and target coords)
  24. write_csv(data, 'data/tidytuesday_201939_school_diversity_raw.csv')
  25. ################################################################################
  26. # combine source and target coords to one single data frame
  27. data_source <- read_csv(
  28. 'data/tidytuesday_201939_school_diversity_preprocessed_source.csv'
  29. )
  30. data_target <- read_csv(
  31. 'data/tidytuesday_201939_school_diversity_preprocessed_target.csv'
  32. )
  33. data_source <- data_source %>%
  34. mutate(sourceX = x, sourceY = y) %>%
  35. select(id, x, y, color, sourceRadius, targetRadius, sourceX, sourceY)
  36. data_target <- data_target %>%
  37. mutate(targetX = x, targetY = y) %>%
  38. select(id, targetX, targetY)
  39. data_source %>%
  40. inner_join(data_target) %>%
  41. write_csv('data/tidytuesday_201939_school_diversity_preprocessed.csv')
  42. ################################################################################
  43. # export d3 animation to video (.mov) using quick time player screen recording
  44. # and ffmpeg to downsample and change frame rate
  45. # ffmpeg -y -i input.mov
  46. # -vf "setpts=0.1*PTS,scale=1200:-1" -r 40000/1001 output.mp4
  47. # -vf "setpts=0.1*PTS,scale=1200:-1" -r 40000/1001 output.gif