/week-51/dog-friendliness.R

https://github.com/gkaramanis/tidytuesday · R · 70 lines · 60 code · 10 blank · 0 comment · 1 complexity · c7ec11bd7a09d89fcdb9d1756824f060 MD5 · raw file

  1. library(tidyverse)
  2. library(here)
  3. library(fuzzyjoin)
  4. library(ggimage)
  5. library(ggrepel)
  6. dog_descriptions <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-12-17/dog_descriptions.csv')
  7. dog_breeds <- tibble(filename = list.files(here("week-51", "png"))) %>%
  8. mutate(
  9. breed_primary = str_to_title(
  10. str_replace_all(filename, c(".png" = "", "-" = " "))
  11. )
  12. )
  13. child_f <- dog_descriptions %>%
  14. count(breed_primary, env_children) %>%
  15. pivot_wider(names_from = env_children, names_prefix ="child_", values_from = n) %>%
  16. mutate(
  17. child_total = child_FALSE + child_TRUE,
  18. child_friendliness = child_TRUE/(child_FALSE + child_TRUE),
  19. )
  20. cat_f <- dog_descriptions %>%
  21. count(breed_primary, env_cats) %>%
  22. pivot_wider(names_from = env_cats, names_prefix ="cat_", values_from = n) %>%
  23. mutate(
  24. cat_total = cat_FALSE + cat_TRUE,
  25. cat_friendliness = cat_TRUE/(cat_FALSE + cat_TRUE),
  26. )
  27. cc_f <- left_join(child_f, cat_f)
  28. fuzzy_dogs <- stringdist_left_join(dog_breeds, cc_f) %>%
  29. filter(!is.na(breed_primary.y)) %>%
  30. filter(!is.na(child_friendliness)) %>%
  31. filter(!is.na(cat_friendliness))
  32. ggplot(fuzzy_dogs) +
  33. geom_point(aes(x = child_friendliness, y = cat_friendliness), size = 10, color = "#fff44f") +
  34. geom_image(aes(x = child_friendliness, y = cat_friendliness, image = paste0(here("week-51", "png"), "/", filename)), size = 0.05) +
  35. geom_label_repel(aes(x = child_friendliness, y = cat_friendliness, label = breed_primary.y), force = 6, point.padding = 3, box.padding = 1.2, segment.color = "black", segment.size = 0.2, fill = "black", color = "#fff44f", family = "IBM Plex Sans Bold", size = 4) +
  36. annotate("text", x = -0.05, y = 0.32, label = toupper("Child- and cat-friendliness\nof 30 dog breeds, as\nassessed* on Petfinder.com,\nfor adoptable dogs\nsince 2003"), hjust = 0, vjust = 1, lineheight = 0.9, size = 8, family = "IBM Plex Sans Bold") +
  37. annotate("text", x = -0.05, y = 0.12, label = toupper("*Based on presumed primary breed,\nas percentage of friendly to total number\nassessed for every breed"), hjust = 0, vjust = 1, lineheight = 0.9, size = 5, family = "IBM Plex Sans Bold") +
  38. labs(
  39. caption = "Source: Petfinder.com via The Pudding | Graphic: Georgios Karamanis\nIcons made by Freepik from www.flaticon.com",
  40. x = "← Less child-friendly More child-friendly →",
  41. y = "← Less cat-friendly More cat-friendly →"
  42. ) +
  43. scale_x_continuous(breaks = c(0, 0.25, 0.5, 0.75, 1), labels = c("0%", "", "50%", "", "100%")) +
  44. scale_y_continuous(breaks = c(0, 0.25, 0.5, 0.75, 1), labels = c("0%", "", "50%", "", "100%")) +
  45. coord_fixed(xlim = c(0, 1), ylim = c(0, 1)) +
  46. theme_minimal(base_family = "IBM Plex Mono Bold") +
  47. theme(
  48. legend.position = "none",
  49. plot.margin = margin(20, 45, 20, 20),
  50. plot.background = element_rect(fill = "#fff44f", color = NA),
  51. panel.grid = element_line(color = "black", size = 0.05),
  52. axis.title = element_text(size = 16, color = "#b1a600"),
  53. axis.title.x = element_text(margin = margin(20, 0, 0, 0)),
  54. axis.title.y = element_text(margin = margin(0, 20, 0, 0)),
  55. axis.text = element_text(size = 12, color = "#b1a600"),
  56. plot.caption = element_text(margin = margin(30, 0, 0, 0), color = "#b1a600")
  57. ) +
  58. ggsave(
  59. here::here("week-51", "plots", "temp", paste0("dog-friendliness", format(Sys.time(), "%Y%m%d_%H%M%S"), ".png")), dpi = 320, height = 12, width = 12
  60. )