Advertisement
Guest User

#TidyTuesday 2021 W35

a guest
Aug 27th, 2021
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.55 KB | None | 0 0
  1. #TT 2021 W35 Lemurs
  2. library(ggforce)
  3. library(ggfx)
  4. library(lubridate)
  5. library(showtext)
  6. library(tidyverse)
  7.  
  8.  
  9. #pull font ----
  10. font_add_google("Fredoka One", "fred")
  11. font_add_google("Comfortaa", "comfortaa")
  12.  
  13.  
  14. #pull data ----
  15. tuesdata <- tidytuesdayR::tt_load(2021, week = 35)
  16.  
  17. lemur <- tuesdata$lemur_data
  18.  
  19. #clean "taxonomy" to match "lemur" for CMED == CMEAD (?)
  20. taxonomy <- tuesdata$taxonomy %>%
  21.   mutate(taxon = ifelse(taxon == "CMEAD", "CMED", taxon))
  22.  
  23.  
  24. #tidy data ----
  25. lemur_tidy <- lemur %>%
  26.   filter(is.na(dod),
  27.          weight_date >= ymd("2019-01-01")) %>%
  28.   group_by(dlc_id) %>%
  29.   slice(1) %>%
  30.   ungroup() %>%
  31.   count(taxon, sort = TRUE, name = "count") %>%
  32.   mutate(share = 100 * (count / sum(count))) %>%
  33.   left_join(taxonomy) %>%
  34.   select(taxon, common_name, count, share)
  35.  
  36.  
  37. #inputs ----
  38. position_x <- c(22, 20, 80, 37, 74, 46, 28, 85, 40, 62, 12, 81, 11, 95, 93, 50)
  39. position_y <- c(43, 80, 87, 10, 68, 90, 62, 12, 27, 13, 26, 25, 96, 54, 34, 74)
  40. hex_colour <- c("#C68147", "#EFC048", "#F1A751", "#3A2610", "#98D0F2", "#DD9345", "#DEDB86", "#FAA247",
  41.                 "#DBC49A", "#C99D79", "#CC8B42", "#D79D58", "#A5744C", "#AF6B43", "#B26948", "#B47D48")
  42.  
  43. #from lemur_tidy
  44. size_colour <- lemur_tidy %>% pull(share) * 10 #scale up by 10 before area-radius conversion
  45. label <- lemur_tidy %>% pull(common_name)
  46.  
  47.  
  48. #df ----
  49. df <- tibble(
  50.   x = position_x,
  51.   y = position_y,
  52.   hex_colour = hex_colour,
  53.   size_colour = sqrt((size_colour / pi)),
  54.   label = label
  55.   ) %>%
  56.   mutate(right = x + size_colour + 1,
  57.          left = x - size_colour - 1,
  58.          size_black = size_colour * 0.1)
  59.  
  60.  
  61. #graphic ----
  62. showtext_auto()
  63. showtext_opts(dpi = 300)
  64.  
  65. #subtitle
  66. subtitle_label <- str_wrap(
  67.   "The Duke Lemur Center houses the most diverse population of lemurs outside their native Madagascar",
  68.   25)
  69.  
  70. #legend
  71. legend_df <- tibble(
  72.   share = c(2.44, 9.76),
  73.   label = c("5 lemurs", "20 lemurs")
  74. ) %>%
  75.   mutate(size_colour = sqrt(((share * 10) / pi)))
  76.  
  77. #plot
  78. ggplot(df) +
  79.   with_inner_glow(
  80.     geom_circle(aes(x0 = left, y0 = y, r = size_colour, fill = hex_colour),
  81.                 colour = NA),
  82.     sigma = 8,
  83.     expand = 2,
  84.     colour = "#A26B30"
  85.   ) +
  86.   with_inner_glow(
  87.    geom_circle(aes(x0 = right, y0 = y, r = size_colour, fill = hex_colour),
  88.                 colour = NA),
  89.     sigma = 8,
  90.     expand = 2,
  91.     colour = "#A26B30"
  92.   ) +
  93.   with_outer_glow(
  94.     geom_circle(aes(x0 = left, y0 = y, r = size_black),
  95.                 fill = "black",
  96.                 colour = NA),
  97.     sigma = 5,
  98.     expand = 5,
  99.     colour = "#A26B30"
  100.   ) +
  101.   with_outer_glow(
  102.     geom_circle(aes(x0 = right, y0 = y, r = size_black),
  103.                 fill = "black",
  104.                 colour = NA),
  105.     sigma = 5,
  106.     expand = 5,
  107.     colour = "#A26B30"
  108.   ) +
  109.   geom_text(aes(x, y - size_colour, label = label),
  110.             nudge_y = -2,
  111.             family = "comfortaa",
  112.             colour = "#F9F3E7",
  113.             size = 2) +
  114.   #legend
  115.   geom_circle(data = legend_df, aes(x0 = c(3, 14), y0 = c((10 - (5.57 - 2.79)), 10), r = size_colour),
  116.               fill = NA,
  117.               colour = "#F9F3E7") +
  118.   geom_text(data = legend_df, aes(x = c(3, 14), y = c(1.5, 1.5), label = label),
  119.             family = "comfortaa",
  120.             colour = "#F9F3E7",
  121.             size = 2) +
  122.   #annotations
  123.   annotate("text", x = 80, y = 55, label = "lemurs", hjust = 1, vjust = 1,
  124.            colour = "#F9F3E7",
  125.            family = "fred",
  126.            size = 13) +
  127.   annotate("text", x = 80, y = 46, label = subtitle_label, hjust = 1, vjust = 1,
  128.            colour = "#F9F3E7",
  129.            family = "comfortaa",
  130.            size = 3) +
  131.   annotate("text", x = 99, y = 1, label = "Note: Includes 205 lemurs with weight records in 2019",
  132.            hjust = 1, vjust = 0,
  133.            colour = "#F9F3E7",
  134.            family = "comfortaa",
  135.            size = 2) +
  136.   annotate("text", x = 99, y = 99, label = "Source: Duke Lemur Center  |  #TidyTuesday 2021 W35  |  @quite_grey",
  137.            hjust = 1, vjust = 1,
  138.            colour = "#F9F3E7",
  139.            family = "comfortaa",
  140.            size = 2) +
  141.   scale_x_continuous(limits = c(0, 100)) +
  142.   scale_y_continuous(limits = c(0, 100)) +
  143.   scale_size_identity() +
  144.   scale_fill_identity() +
  145.   coord_fixed() +
  146.   theme_void() +
  147.   theme(legend.position = "none",
  148.         panel.background = element_rect(fill = "#1E1722", colour = "#1E1722"),
  149.         plot.background = element_rect(fill = "#1E1722", colour = "#1E1722"))
  150.  
  151.  
  152. #save ----
  153. ggsave("./output/plot/lemur.png", height = 6, width = 6, unit = "in")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement