Discrete Timeline

Visualising periods using a Timeline with discrete axis breaks.

R code

Sys.setlocale("LC_ALL", 'en_US.UTF-8')

# Libraries -------------------------------------------------------------------

library(tidyverse)

# Data -------------------------------------------------------------------

events <- tibble(
  event = c("Event A", "Event B", "Event C", "Event D"),
  start = c(1990, 1996, 1998, 2004),
  end = c(1996, 1998, 2004, 2012)
)

# Theme -------------------------------------------------------------------

fontFamily <- "Zilla Slab"
colrBackground <- "#fafafa"
colrPrim <- "#2a2a2a"
colrSub <- "#e1e1e1"
colrGeom <- "black"
titleFontSize <- 12
subtitleFontSize <- 8
captionFontSize <- 8
axisFontSize <- 8
fix_annotationFontSize <- 2.5
axisType <- "dashed"

update_geom_defaults("text", list(family = fontFamily, size = axisFontSize/fix_annotationFontSize)) # update default geom text

events_theme <- theme(
  text = element_text(family = fontFamily),
  panel.background =  element_rect(fill = NA, colour = NA), 
  panel.border = element_blank(),
  panel.grid = element_blank(),
  panel.grid.major.x = element_line(size = .2, linetype = axisType, color = colrSub),
  plot.title = element_text(size = titleFontSize, face = "italic", color = colrPrim, hjust=0, margin = unit(c(0, 0, 40, 0), "pt") ),
  plot.subtitle = element_text(size = subtitleFontSize,  color = colrPrim, face = "plain", hjust=0, margin = unit(c(0, 0, 0, 0), "pt") ),
  plot.caption = element_text(size = captionFontSize,  color = colrPrim, face = "italic", hjust = 1, margin = unit(c(20, 0, 0, 0), "pt") ),
  plot.background = element_rect(fill = colrBackground),
  plot.margin = unit( c(40, 24, 24, 24), "points"),
  axis.ticks = element_blank(),
  axis.ticks.length = unit(3, "pt"),
  axis.text = element_text(size = axisFontSize,  color = colrPrim, face = "italic"),
  axis.text.x = element_text(margin = margin(t = 8)),
  axis.text.y = element_blank()
)

# Plot -------------------------------------------------------------------

plot <-ggplot(events, aes(y = event)) +
  geom_segment(aes(x = start, xend = end, y = event, yend = event), color = colrGeom, size = 0.33) +
  #geom_point(aes(x = start)) +
  #geom_point(aes(x = end)) +
  geom_text(label = "▶" , aes(x = start), hjust = 0.15, vjust = 0.35, size = 4, color = colrGeom) +
  geom_text(label = "◀" , aes(x = end), hjust = 0.8, vjust = 0.35, size = 4, color = colrGeom) +
  geom_text(aes(label = event, x = start+((end-start)/2)), hjust = 0.5, vjust = -1.5) +
  scale_x_continuous(breaks = union(events$start, events$end)) +
  labs(title = "Timeline with discrete axis breaks", subtitle = "", x = "", y = "", caption = "inspired by http://r4ds.had.co.nz/graphics-for-communication.html#scales") +
  events_theme

plot

# Export -------------------------------------------------------------------

ggsave(plot, width = 6, height = 4, device = cairo_pdf, filename = "export/Discrete breaks in X axis.pdf")
ggsave(plot, width = 6, height = 4, dpi = "retina", filename = "export/Discrete breaks in X axis.png")

Keyshape prototype

The transition duration for each interval reflects its relative size on the timeline.