/tidytuesday_201932_bob_ross_paintings.r

https://github.com/spren9er/tidytuesday · R · 121 lines · 106 code · 15 blank · 0 comment · 3 complexity · 79d96c7cc409f7ed45256bca2fdb8c7d MD5 · raw file

  1. library(tidyverse)
  2. path <-
  3. paste0(
  4. 'https://raw.githubusercontent.com/rfordatascience/tidytuesday/',
  5. 'master/data/2019/2019-08-06/'
  6. )
  7. data <- read_csv(paste0(path, 'bob-ross.csv')) %>%
  8. janitor::clean_names() %>%
  9. mutate(
  10. episode = str_replace(episode, 'S0', 'S'),
  11. episode = str_replace(episode, 'E0', 'E'),
  12. title = str_to_title(str_replace_all(title, '"', ''))
  13. ) %>%
  14. select(-contains('STEVE'), -contains('DIANE'), -contains('FRAME'))
  15. colors <- read_csv(
  16. 'data/tidytuesday_201932_bob_ross_paintings.csv',
  17. col_names = c('episode', 'title', 'color', 'color_name')
  18. ) %>%
  19. filter(!color %in% c('#FFFFFF', '#000000'))
  20. ordered_colors <- c(
  21. 'Phthalo Blue', 'Prussian Blue', 'Phthalo Green', 'Sap Green',
  22. 'Van Dyke Brown', 'Alizarin Crimson', 'Dark Sienna', 'Burnt Umber',
  23. 'Bright Red', 'Indian Red', 'Yellow Ochre', 'Indian Yellow', 'Cadmium Yellow'
  24. )
  25. palette <- colors %>%
  26. distinct(color, color_name) %>%
  27. mutate(color_name = fct_relevel(color_name, ordered_colors)) %>%
  28. arrange(color_name)
  29. objects <- data %>%
  30. pivot_longer(
  31. -one_of('episode', 'title'),
  32. names_to = 'object', values_to = 'painted'
  33. ) %>%
  34. mutate(object = str_to_title(str_replace_all(object, '_', ' '))) %>%
  35. filter(object != 'Guest')
  36. top_objects <- objects %>%
  37. filter(painted == 1) %>%
  38. count(object, name = 'object_total', sort = TRUE) %>%
  39. filter(object_total >= 10) %>%
  40. mutate(object = fct_reorder(object, -object_total))
  41. objects_colors <- objects %>%
  42. right_join(top_objects, by = 'object') %>%
  43. inner_join(select(colors, -title), by = 'episode') %>%
  44. count(object, color_name, painted, name = 'total', sort = TRUE) %>%
  45. filter(painted == 1) %>%
  46. select(-painted) %>%
  47. pivot_wider(names_from = color_name, values_from = total) %>%
  48. replace(is.na(.), 0)
  49. objects_colors_mtx <- as.matrix(column_to_rownames(objects_colors, 'object'))
  50. chi2 <- chisq.test(objects_colors_mtx, correct = F)
  51. residuals <- as.tibble(chi2$residuals, rownames = 'object')
  52. cut_residuals <- residuals %>%
  53. pivot_longer(-object, names_to = 'color', values_to = 'residual') %>%
  54. mutate(
  55. cut_residual = ifelse(residual > 1, 1, residual),
  56. cut_residual = ifelse(residual < 0, 0, cut_residual)
  57. )
  58. dist_mtx <- cut_residuals %>%
  59. pivot_wider(object, names_from = color, values_from = cut_residual) %>%
  60. column_to_rownames('object') %>%
  61. as.matrix() %>%
  62. dist()
  63. cluster <- hclust(dist_mtx)
  64. reordered_objects <- cluster$labels[cluster$order]
  65. cut_residuals %>%
  66. mutate(
  67. color = fct_relevel(color, as.character(palette$color_name)),
  68. object = fct_relevel(object, reordered_objects)
  69. ) %>%
  70. filter(cut_residual > 0) %>%
  71. ggplot(aes(x = color, y = object, fill = color, alpha = cut_residual)) +
  72. geom_tile(width = 0.9, height = 0.9, show.legend = FALSE) +
  73. scale_fill_manual(values = palette$color) +
  74. scale_x_discrete(position = 'top') +
  75. scale_alpha_continuous(range = c(0, 1)) +
  76. labs(
  77. x = '',
  78. y = '',
  79. title = 'Elements & Colors in Bob Ross Paintings',
  80. subtitle = '#tidytuesday 32 | 2019',
  81. caption = '© 2019 spren9er'
  82. ) +
  83. theme(
  84. text = element_text(family = 'Dosis'),
  85. axis.text.x = element_text(size = 6.5, angle = 90, hjust = 0),
  86. axis.text.y = element_text(size = 6.5),
  87. axis.text.x.top = element_text(vjust = 0.5),
  88. panel.grid.minor = element_blank(),
  89. panel.grid.major = element_blank(),
  90. plot.title = element_text(
  91. size = 8, hjust = -0.53, margin = margin(t = 10, b = 5),
  92. color = '#555555'
  93. ),
  94. plot.subtitle = element_text(
  95. size = 7, hjust = -0.28, face = 'plain', color = '#555555'
  96. ),
  97. plot.caption = element_text(
  98. color = '#cccccc', size = 5, margin = margin(t = -6), hjust = 0.9825,
  99. face = 'plain'
  100. ),
  101. plot.margin = margin(t = 10, r = 10, b = 10, l = -6)
  102. )
  103. ggsave(
  104. 'images/tidytuesday_201932_bob_ross_paintings.png',
  105. width = 3.8, height = 8, dpi = 300
  106. )