## -----------------------------------------------------------------------------
#| label: setup
library(ggplot2)
library(ggindex)

cars <- mtcars
cars$model <- rownames(cars)
cars$rank <- rank(-cars$mpg, ties.method = "first")


## -----------------------------------------------------------------------------
#| label: scales
cars$best <- cars$mpg == ave(cars$mpg, cars$cyl, FUN = max)

ggplot(cars, aes(wt, mpg)) +
  geom_point(colour = "grey60") +
  geom_focus(
    aes(label = rank, focus = best, info = model, fill = factor(cyl)),
    shape = 22,
    size = 7
  ) +
  scale_fill_brewer("Cylinders", palette = "Set2") +
  scale_info_discrete("Best in class") +
  theme_minimal()


## -----------------------------------------------------------------------------
#| label: text
ggplot(cars, aes(wt, mpg)) +
  geom_point(colour = "grey60") +
  geom_focus(
    aes(label = rank, focus = rank <= 5, info = model),
    fill = "grey20",
    text.colour = "white",
    fontface = "bold"
  ) +
  theme_minimal()


## -----------------------------------------------------------------------------
#| label: theme
ggplot(cars, aes(wt, mpg)) +
  geom_point(colour = "grey50") +
  geom_focus(aes(label = rank, focus = rank <= 5, info = model)) +
  theme_minimal(ink = "white", paper = "grey15")


## -----------------------------------------------------------------------------
#| label: facets
ggplot(cars, aes(wt, mpg)) +
  geom_point(colour = "grey60") +
  geom_focus(aes(label = rank, focus = rank <= 3, info = model)) +
  facet_wrap(vars(cyl), labeller = label_both) +
  theme_minimal()

