## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 4.5,
  dpi = 150
)

## ----setup--------------------------------------------------------------------
library(ggplot2)
library(ggmemo)

revenue <- data.frame(
  quarter = factor(c("Q1", "Q2", "Q3", "Q4"),
                   levels = c("Q1", "Q2", "Q3", "Q4")),
  revenue = c(120, 145, 132, 158)
)

## ----bare-chart---------------------------------------------------------------
p <- ggplot(revenue, aes(x = quarter, y = revenue)) +
  geom_col(fill = "steelblue", width = 0.6) +
  labs(title = "2024 Quarterly Revenue ($K)", x = NULL, y = NULL) +
  theme_minimal()
p

## ----callout------------------------------------------------------------------
p +
  annotate_callout(
    revenue,
    where = quarter == "Q4",
    label = "Record quarter",
    position = "top-left"
  )

## ----change-percent-----------------------------------------------------------
p +
  annotate_change(
    revenue,
    from = quarter == "Q1",
    to = quarter == "Q4",
    value = revenue
  )

## ----change-absolute----------------------------------------------------------
p +
  annotate_change(
    revenue,
    from = quarter == "Q1",
    to = quarter == "Q4",
    value = revenue,
    format = "absolute"
  )

## ----change-points------------------------------------------------------------
rates <- data.frame(
  year = 2020:2023,
  rate = c(3.5, 8.1, 5.4, 3.7)
)

ggplot(rates, aes(x = year, y = rate)) +
  geom_line() +
  geom_point() +
  annotate_change(
    rates,
    from = year == 2020,
    to = year == 2021,
    value = rate,
    format = "points"
  ) +
  labs(title = "Unemployment Rate", y = "Rate (%)") +
  theme_minimal()

## ----combined-----------------------------------------------------------------
ggplot(revenue, aes(x = quarter, y = revenue)) +
  geom_col(fill = "steelblue", width = 0.6) +
  annotate_callout(
    revenue,
    where = quarter == "Q4",
    label = "Record quarter",
    position = "top-left"
  ) +
  annotate_change(
    revenue,
    from = quarter == "Q1",
    to = quarter == "Q4",
    value = revenue
  ) +
  labs(title = "2024 Quarterly Revenue ($K)", x = NULL, y = NULL) +
  theme_minimal()

## ----multiple-----------------------------------------------------------------
ggplot(revenue, aes(x = quarter, y = revenue)) +
  geom_col(fill = "grey70", width = 0.6) +
  annotate_change(revenue, from = quarter == "Q1",
                  to = quarter == "Q2", value = revenue) +
  annotate_change(revenue, from = quarter == "Q2",
                  to = quarter == "Q3", value = revenue) +
  annotate_change(revenue, from = quarter == "Q3",
                  to = quarter == "Q4", value = revenue) +
  labs(title = "Quarter-over-Quarter Changes", x = NULL, y = NULL) +
  theme_minimal()

## ----time-series--------------------------------------------------------------
ggplot(economics, aes(x = date, y = psavert)) +
  geom_line(colour = "grey40") +
  annotate_callout(
    economics,
    where = date == as.Date("2005-07-01"),
    label = "All-time low",
    nudge = c(365, 1)
  ) +
  annotate_change(
    economics,
    from = date == as.Date("2005-07-01"),
    to = date == as.Date("2012-12-01"),
    value = psavert,
    format = "points"
  ) +
  labs(
    title = "U.S. Personal Savings Rate",
    subtitle = "Recovery after the 2005 low",
    x = NULL, y = "Savings rate (%)"
  ) +
  theme_minimal()

## ----custom-colors------------------------------------------------------------
p +
  annotate_change(
    revenue,
    from = quarter == "Q1",
    to = quarter == "Q4",
    value = revenue,
    colors = c(up = "#1B9E77", down = "#D95F02", flat = "#7570B3")
  )

## ----arrow-styling------------------------------------------------------------
p +
  annotate_callout(
    revenue,
    where = quarter == "Q4",
    label = "Record quarter",
    position = "top-left",
    arrow = NULL
  ) +
  annotate_change(
    revenue,
    from = quarter == "Q3",
    to = quarter == "Q4",
    value = revenue,
    arrow_type = "closed",
    arrow_pad = 0.2,
    curvature = -0.3
  )

## ----custom-style-------------------------------------------------------------
p +
  annotate_callout(
    revenue,
    where = quarter == "Q4",
    label = "Record quarter",
    position = "top-left",
    size = 5,
    fill = "lightyellow",
    colour = "grey30"
  )

## ----nudge--------------------------------------------------------------------
ggplot(economics, aes(x = date, y = unemploy)) +
  geom_line() +
  annotate_callout(
    economics,
    where = date == as.Date("2009-10-01"),
    label = "Peak unemployment",
    nudge = c(800, 1000)
  ) +
  theme_minimal()

## ----nba-clutch---------------------------------------------------------------
scoring <- data.frame(
  Quarter = rep(c("Q1", "Q2", "Q3", "Q4"), 4),
  Team = rep(c("Knicks", "Knicks", "Spurs", "Spurs"), each = 4),
  Player = rep(c("Brunson", "Rest of Knicks", "Wemby", "Rest of Spurs"), each = 4),
  Points = c(
    5, 7, 5, 13,
    23, 20, 20, 12,
    8, 9, 7, 4,
    16, 17, 19, 15
  )
)

scoring$Quarter <- factor(scoring$Quarter, levels = c("Q1", "Q2", "Q3", "Q4"))
scoring$Player <- factor(scoring$Player,
  levels = c("Rest of Spurs", "Wemby", "Rest of Knicks", "Brunson"))

scoring$x_pos <- as.numeric(scoring$Quarter) +
  ifelse(scoring$Team == "Knicks", -0.2, 0.2)

team_totals <- aggregate(Points ~ Quarter + Team + x_pos, data = scoring, FUN = sum)
team_totals <- team_totals[!duplicated(team_totals[, c("Quarter", "Team")]), ]

ggplot(scoring, aes(x = x_pos, y = Points, fill = Player)) +
  geom_col(width = 0.35) +
  geom_text(
    data = team_totals,
    aes(x = x_pos, y = Points, label = Points, fill = NULL),
    vjust = -0.4, size = 5, fontface = "bold"
  ) +
  scale_fill_manual(
    values = c(
      "Brunson" = "#E86A00",
      "Rest of Knicks" = "#FDCB8B",
      "Wemby" = "#6D6D6D",
      "Rest of Spurs" = "#B8B8B8"
    ),
    name = NULL
  ) +
  scale_x_continuous(breaks = 1:4, labels = c("Q1", "Q2", "Q3", "Q4")) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.12))) +
  annotate_change(
    data.frame(x_pos = c(2.8, 3.8), Points = c(5, 13)),
    from = x_pos == 2.8,
    to = x_pos == 3.8,
    value = Points,
    format = "percent",
    expand_y = FALSE,
    label = "20% → 52% of team pts",
    colors = c(up = "#E86A00", down = "#B22222", flat = "#808080")
  ) +
  coord_cartesian(clip = "off") +
  labs(
    title = "Quarter-by-Quarter Scoring — Knicks vs Spurs",
    x = NULL, y = "Points"
  ) +
  theme_minimal(base_size = 14) +
  theme(
    legend.position = "bottom",
    panel.grid.major.x = element_blank(),
    plot.margin = margin(10, 20, 10, 10)
  )

