## -----------------------------------------------------------------------------
#| label: setup
library(ggplot2)
library(ggindex)


## -----------------------------------------------------------------------------
#| label: basic
sites <- data.frame(
  x = c(6, 7, 8, 9, 10),
  y = c(6, 9, 7, 10, 8),
  name = c("Library", "Main hall", "Sports centre", "Cafeteria", "Car park")
)

ggplot(sites, aes(x, y, info = name)) +
  geom_focus() +
  theme_minimal()


## -----------------------------------------------------------------------------
#| label: label
sites$code <- c(12, 3, 104, 7, 45)

ggplot(sites, aes(x, y, label = code, info = name)) +
  geom_focus() +
  theme_minimal()


## -----------------------------------------------------------------------------
#| label: focus
cars <- mtcars
cars$model <- rownames(cars)
cars$rank <- rank(-cars$mpg, ties.method = "first")

ggplot(cars, aes(wt, mpg)) +
  geom_point(colour = "grey60") +
  geom_focus(aes(label = rank, focus = rank <= 5, info = model)) +
  scale_info_discrete("Most efficient") +
  labs(x = "Weight (1000 lbs)", y = "Miles per gallon") +
  theme_minimal()


## -----------------------------------------------------------------------------
#| label: nudge
ggplot(cars, aes(wt, mpg)) +
  geom_point(colour = "grey60") +
  geom_focus(
    aes(
      label = rank,
      focus = rank <= 5,
      info = model,
      nudge_x = ifelse(model == "Lotus Europa", -0.12, 0)
    ),
    position = "nudge"
  ) +
  theme_minimal()


## -----------------------------------------------------------------------------
#| label: map
#| eval: !expr requireNamespace("sf", quietly = TRUE)
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
nc <- sf::st_transform(nc, 32119)
nc_large <- nc[nc$AREA > 0.2, ]

ggplot(nc) +
  geom_sf(fill = "grey90", colour = "white") +
  geom_sf_focus(aes(info = NAME), data = nc_large) +
  scale_info_discrete("Largest counties") +
  theme_void()

