## -----------------------------------------------------------------------------
#| label: setup
library(ggplot2)
library(ggindex)


## -----------------------------------------------------------------------------
#| label: line
events <- data.frame(
  date = as.Date(c("2001-03-01", "2007-12-01", "2008-09-15")),
  event = c(
    "Dot-com recession begins",
    "Great Recession begins",
    "Lehman Brothers collapses"
  )
)
events$unemploy <- approx(economics$date, economics$unemploy, events$date)$y

ggplot(economics, aes(date, unemploy)) +
  geom_line() +
  geom_focus(aes(info = event), data = events) +
  scale_info_discrete("Events") +
  labs(x = NULL, y = "Unemployed (thousands)") +
  theme_minimal()


## -----------------------------------------------------------------------------
#| label: bar
classes <- as.data.frame(table(class = mpg$class), responseName = "n")

ggplot(classes, aes(n, class)) +
  geom_col(fill = "grey80") +
  geom_focus(
    aes(info = class, focus = n > 40),
    position = position_nudge(x = 3)
  ) +
  scale_info_discrete("Most common") +
  labs(x = "Models", y = NULL) +
  theme_minimal()


## -----------------------------------------------------------------------------
#| label: box
ggplot(mpg, aes(class, hwy)) +
  geom_boxplot(outliers = FALSE) +
  geom_focus(aes(info = model, focus = hwy > 40)) +
  scale_info_discrete("Outliers") +
  labs(x = NULL, y = "Highway miles per gallon") +
  theme_minimal()


## -----------------------------------------------------------------------------
#| label: density
waiting <- faithful$waiting
dens <- density(waiting)
centres <- data.frame(
  stat = c("Mean", "Median", "Mode"),
  x = c(mean(waiting), median(waiting), dens$x[which.max(dens$y)])
)
centres$y <- approx(dens$x, dens$y, centres$x)$y
centres$info <- sprintf("%s: %.1f min", centres$stat, centres$x)

ggplot(faithful, aes(waiting)) +
  geom_density(fill = "grey90") +
  geom_segment(
    aes(x = x, xend = x, y = 0, yend = y),
    data = centres,
    colour = "grey50",
    linetype = "dashed"
  ) +
  geom_focus(aes(x, y, info = info), data = centres) +
  scale_info_discrete(NULL) +
  labs(x = "Minutes to next eruption", y = "Density") +
  theme_minimal()


## -----------------------------------------------------------------------------
#| label: ecdf
quartiles <- data.frame(p = c(0.25, 0.5, 0.75))
quartiles$waiting <- quantile(waiting, quartiles$p, type = 1)
quartiles$label <- quartiles$p * 100
quartiles$info <- sprintf(
  "%g%% erupt within %g min",
  quartiles$label,
  quartiles$waiting
)

ggplot(faithful, aes(waiting)) +
  stat_ecdf(geom = "step") +
  geom_focus(aes(y = p, label = label, info = info), data = quartiles) +
  scale_info_discrete("Quartiles") +
  labs(x = "Minutes to next eruption", y = "Cumulative probability") +
  theme_minimal()


## -----------------------------------------------------------------------------
#| label: summary
ggplot(mpg, aes(class, hwy)) +
  geom_jitter(width = 0.2, height = 0, colour = "grey70") +
  geom_focus(
    aes(label = after_stat(round(y))),
    stat = "summary",
    fun = mean
  ) +
  labs(x = NULL, y = "Highway miles per gallon") +
  theme_minimal()


## -----------------------------------------------------------------------------
#| label: radial
ggplot(classes, aes(class, n)) +
  geom_col(fill = "grey80") +
  geom_focus(aes(info = class, focus = n > 40)) +
  scale_info_discrete("Most common") +
  coord_radial() +
  labs(x = NULL, y = NULL) +
  theme_minimal()

