Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #TT 2021 W35 Lemurs
- library(ggforce)
- library(ggfx)
- library(lubridate)
- library(showtext)
- library(tidyverse)
- #pull font ----
- font_add_google("Fredoka One", "fred")
- font_add_google("Comfortaa", "comfortaa")
- #pull data ----
- tuesdata <- tidytuesdayR::tt_load(2021, week = 35)
- lemur <- tuesdata$lemur_data
- #clean "taxonomy" to match "lemur" for CMED == CMEAD (?)
- taxonomy <- tuesdata$taxonomy %>%
- mutate(taxon = ifelse(taxon == "CMEAD", "CMED", taxon))
- #tidy data ----
- lemur_tidy <- lemur %>%
- filter(is.na(dod),
- weight_date >= ymd("2019-01-01")) %>%
- group_by(dlc_id) %>%
- slice(1) %>%
- ungroup() %>%
- count(taxon, sort = TRUE, name = "count") %>%
- mutate(share = 100 * (count / sum(count))) %>%
- left_join(taxonomy) %>%
- select(taxon, common_name, count, share)
- #inputs ----
- position_x <- c(22, 20, 80, 37, 74, 46, 28, 85, 40, 62, 12, 81, 11, 95, 93, 50)
- position_y <- c(43, 80, 87, 10, 68, 90, 62, 12, 27, 13, 26, 25, 96, 54, 34, 74)
- hex_colour <- c("#C68147", "#EFC048", "#F1A751", "#3A2610", "#98D0F2", "#DD9345", "#DEDB86", "#FAA247",
- "#DBC49A", "#C99D79", "#CC8B42", "#D79D58", "#A5744C", "#AF6B43", "#B26948", "#B47D48")
- #from lemur_tidy
- size_colour <- lemur_tidy %>% pull(share) * 10 #scale up by 10 before area-radius conversion
- label <- lemur_tidy %>% pull(common_name)
- #df ----
- df <- tibble(
- x = position_x,
- y = position_y,
- hex_colour = hex_colour,
- size_colour = sqrt((size_colour / pi)),
- label = label
- ) %>%
- mutate(right = x + size_colour + 1,
- left = x - size_colour - 1,
- size_black = size_colour * 0.1)
- #graphic ----
- showtext_auto()
- showtext_opts(dpi = 300)
- #subtitle
- subtitle_label <- str_wrap(
- "The Duke Lemur Center houses the most diverse population of lemurs outside their native Madagascar",
- 25)
- #legend
- legend_df <- tibble(
- share = c(2.44, 9.76),
- label = c("5 lemurs", "20 lemurs")
- ) %>%
- mutate(size_colour = sqrt(((share * 10) / pi)))
- #plot
- ggplot(df) +
- with_inner_glow(
- geom_circle(aes(x0 = left, y0 = y, r = size_colour, fill = hex_colour),
- colour = NA),
- sigma = 8,
- expand = 2,
- colour = "#A26B30"
- ) +
- with_inner_glow(
- geom_circle(aes(x0 = right, y0 = y, r = size_colour, fill = hex_colour),
- colour = NA),
- sigma = 8,
- expand = 2,
- colour = "#A26B30"
- ) +
- with_outer_glow(
- geom_circle(aes(x0 = left, y0 = y, r = size_black),
- fill = "black",
- colour = NA),
- sigma = 5,
- expand = 5,
- colour = "#A26B30"
- ) +
- with_outer_glow(
- geom_circle(aes(x0 = right, y0 = y, r = size_black),
- fill = "black",
- colour = NA),
- sigma = 5,
- expand = 5,
- colour = "#A26B30"
- ) +
- geom_text(aes(x, y - size_colour, label = label),
- nudge_y = -2,
- family = "comfortaa",
- colour = "#F9F3E7",
- size = 2) +
- #legend
- geom_circle(data = legend_df, aes(x0 = c(3, 14), y0 = c((10 - (5.57 - 2.79)), 10), r = size_colour),
- fill = NA,
- colour = "#F9F3E7") +
- geom_text(data = legend_df, aes(x = c(3, 14), y = c(1.5, 1.5), label = label),
- family = "comfortaa",
- colour = "#F9F3E7",
- size = 2) +
- #annotations
- annotate("text", x = 80, y = 55, label = "lemurs", hjust = 1, vjust = 1,
- colour = "#F9F3E7",
- family = "fred",
- size = 13) +
- annotate("text", x = 80, y = 46, label = subtitle_label, hjust = 1, vjust = 1,
- colour = "#F9F3E7",
- family = "comfortaa",
- size = 3) +
- annotate("text", x = 99, y = 1, label = "Note: Includes 205 lemurs with weight records in 2019",
- hjust = 1, vjust = 0,
- colour = "#F9F3E7",
- family = "comfortaa",
- size = 2) +
- annotate("text", x = 99, y = 99, label = "Source: Duke Lemur Center | #TidyTuesday 2021 W35 | @quite_grey",
- hjust = 1, vjust = 1,
- colour = "#F9F3E7",
- family = "comfortaa",
- size = 2) +
- scale_x_continuous(limits = c(0, 100)) +
- scale_y_continuous(limits = c(0, 100)) +
- scale_size_identity() +
- scale_fill_identity() +
- coord_fixed() +
- theme_void() +
- theme(legend.position = "none",
- panel.background = element_rect(fill = "#1E1722", colour = "#1E1722"),
- plot.background = element_rect(fill = "#1E1722", colour = "#1E1722"))
- #save ----
- ggsave("./output/plot/lemur.png", height = 6, width = 6, unit = "in")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement