21  Down/Upwards

Setup
library(tidyverse) # for data wrangling
library(lubridate) # for dates
library(jsonlite)  # for reading JSON files
library(showtext)  # for custom fonts
library(ggtext)    # for adding the twitter logo

theme_set(theme_minimal(base_size = 16))
font_add("fa-brands", "fonts/fa-brands-400.ttf")
showtext_auto()

21.1 GitHub Data

I used the GitHub API to get info on all my repositories.

Code
urls <- paste0("https://api.github.com/users/debruine/repos?page=", 1:3)
repos <- map_df(urls, read_json, simplifyVector = TRUE)

saveRDS(repos, "data/debruine_github_repos.rds")
Code
repos <- readRDS("data/debruine_github_repos.rds")

21.2 Initial Plot

I’m going to try to make a timeline of when I created each repository.

Code
ggplot(repos, aes(x = created_at, label = name)) +
  geom_text(angle = 45, y = 0)

21.3 Convert to date

Nope. I checked the data types and created_at is a character string, so I need to convert it to a date.

Code
repos$date <- lubridate::as_date(repos$created_at)

I also set hjust = 0 so the labels are left-justified rather than centered. Better, but there’s still too much overlap.

Code
ggplot(repos, aes(x = date, label = name)) +
  geom_text(angle = 45, y = 0, hjust = 0)

21.4 Alternate labels

Let’s try alternating the y and hjust values.

Code
repos <-  arrange(repos, date)
repos$y <- rep(c(.1, -.1), length.out = nrow(repos))
repos$hjust <- rep(0:1, length.out = nrow(repos))
Code
ggplot(repos, aes(x = date, label = name, y = y, hjust = hjust)) +
  geom_text(angle = 45)

OK, I need to control the y-axis limits. I’ll also add a horizontal line for the timeline and a line from each label to the timeline.

Code
ggplot(repos, aes(x = date, label = name, y = y, hjust = hjust)) +
  geom_text(angle = 45) +
  geom_segment(aes(xend = date), yend = 0, color = "grey") +
  geom_hline(yintercept = 0, size = 3, color = "dodgerblue") +
  scale_x_date(expand = expansion(c(.1, .2))) +
  coord_cartesian(ylim = c(-.6, .6))

21.5 Facet by year

Better, but still crowded. What If I facet by year?

Code
repos$year <- year(repos$date)

ggplot(repos, aes(x = date, label = name, y = y, hjust = hjust)) +
  geom_text(angle = 45) +
  geom_segment(aes(xend = date), yend = 0, color = "grey") +
  geom_hline(yintercept = 0, size = 3, color = "dodgerblue") +
  scale_x_date(expand = expansion(c(.2, .2))) +
  coord_cartesian(ylim = c(-.6, .6)) +
  facet_grid(year~.)

The y-axis scale needs to be set to “free”. This doesn’t work with facet_grid(), so I’ll change it to facet_wrap(). I also changed the figure dimensions to 16x16 and set clip = "off" in coord_cartesian() to avoid some of the longer labels from getting clipped.

Code
ggplot(repos, aes(x = date, label = name, y = y, hjust = hjust)) +
  geom_text(angle = 45) +
  geom_segment(aes(xend = date), yend = 0, color = "grey") +
  geom_hline(yintercept = 0, size = 3, color = "dodgerblue") +
  scale_x_date(expand = expansion(c(.2, .2))) +
  coord_cartesian(ylim = c(-.6, .6), clip = "off") +
  facet_wrap(~year, ncol = 1, scales = "free")

The scales aren’t all the same. I’m going to use a hacky solution and add labels for January 1 and December 31 every year, but give them a blank string for a name.

Code
new_dates <- c(paste0(2015:2022, "-01-01"),
               paste0(2015:2022, "-12-31"))

new_entries <- tibble(
  name = "",
  y = 0, hjust = 0, 
  date = as_date(new_dates),
  year = year(as_date(new_dates))
)

repos2 <- bind_rows(repos, new_entries)

I’m also going to reduce the label angle to 30 degrees and make the timeline a different colour each year. I couldn’t set the colour of geom_hline() using aes() unless I was also setting yintercept in the mapping.

Code
ggplot(repos2, aes(x = date, label = name, y = y, hjust = hjust)) +
  geom_text(angle = 30) +
  geom_segment(aes(xend = date), yend = 0, color = "grey") +
  geom_hline(aes(yintercept = 0, color = as.factor(year)), 
             size = 3, show.legend = FALSE) +
  scale_x_date(expand = expansion(c(.2, .2))) +
  coord_cartesian(ylim = c(-.6, .6), clip = "off") +
  facet_wrap(~year, ncol = 1, scales = "free")

21.6 Manually adjust labels

That’s good until the last few months. I’ve been making too many repos! I’ll manually adjust those.

Code
repos3 <- repos2 %>%
  mutate(y = case_when(
    name == "msc-data-skills" ~ -.2,
    name == "NCOD2021" ~ -.2, 
    name == "30DCC-2022" ~ -.2,
    name == "APA_TOP" ~ -.3,
    name == "knitr" ~ .2,
    name == "statistical_inferences" ~ .3,
    name == "waffle" ~ .37,
    name == "shiny2022" ~ .41,
    name == "ug1-practical" ~ .2,
    name == "lmem_sim_private" ~ .2,
    name == "webexercises" ~ .2,
    name == "reprores_2021" ~ .22,
    name == "debruine.github.io" ~ .2,
    name == "statswithr" ~ .15,
    name == "quarto_demo" ~ -.35,
    TRUE ~ y
  ))

I used the code from Day 2.4 to change the ugly green to yellow.

Code
ggplotColours <- function(n, h = c(0, 360) + 15){
  h[2] <- h[2] - 360/n
  hcl(h = (seq(h[1], h[2], length = n)), c = 100, l = 65)
}
linecol <- ggplotColours(8)
linecol[3] <- "#F5C748"

I’m also going to remove the x-axis expansions, since it doesn’t make sense to have dates before January or after December each year, and add a plot margin using theme() to make room for labels that go off the edge a bit.

Code
ggplot(repos3, aes(x = date, label = name, y = y, 
             hjust = hjust, vjust = hjust)) +
  geom_text(angle = 30) +
  geom_segment(aes(xend = date), yend = 0, color = "grey20") +
  geom_hline(aes(yintercept = 0, color = as.factor(year)), 
             size = 3, show.legend = FALSE) +
  scale_color_manual(values = linecol) +
  scale_x_date(date_breaks = "1 month",
               date_labels = "%b",
               expand = expansion(c(0, 0))) +
  coord_cartesian(ylim = c(-.6, .6), clip = "off") +
  facet_wrap(~year, ncol = 1, scales = "free", strip.position = "left") +
  labs(x = NULL, y = NULL, 
       title = "<span style='font-family:fa-brands'>&#xf09b;</span> GitHub Repository Creation") +
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank(),
        plot.title = element_markdown(size = 30, vjust = 1),
        strip.text = element_text(size = 20),
        plot.margin = unit(c(0.1, 1, 0.1, 0.1), "inches")
  )

A timeline of github repository creation from 2015 to 2022. The first few years only have a few repos, which the most recent months have tens of repos.