rm(list = ls())
#remotes::install_github("hughjonesd/ggmagnify")
library(ggmagnify)
library(ggfx)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.2     ✔ purrr   1.0.1
## ✔ tibble  3.2.1     ✔ dplyr   1.1.2
## ✔ tidyr   1.3.0     ✔ stringr 1.5.0
## ✔ readr   2.1.4     ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ purrr::compose() masks ggmagnify::compose()
## ✖ dplyr::filter()  masks stats::filter()
## ✖ dplyr::lag()     masks stats::lag()
dim(ChickWeight)
## [1] 578   4
head(ChickWeight)
##   weight Time Chick Diet
## 1     42    0     1    1
## 2     51    2     1    1
## 3     59    4     1    1
## 4     64    6     1    1
## 5     76    8     1    1
## 6     93   10     1    1
ggp <- ggplot(ChickWeight, aes(Time, weight, group = Chick, color = Diet)) +
  geom_line()
ggp

ggm <- ggmagnify(ggp,
                 xlim = c(0, 5), ylim = c(30, 80),
                 inset_xlim = c(0, 15), inset_ylim = c(200, 350))
ggm

ggmagnify(ggp,
          xlim = c(0, 5), ylim = c(30, 80),
          inset_xlim = c(0, 15), inset_ylim = c(200, 350),
          axes = TRUE)

ggmagnify(ggp,
          xlim = c(0, 5), ylim = c(30, 80),
          inset_xlim = c(0, 15), inset_ylim = c(200, 350),
          shadow = TRUE)

booms <- ggplot(faithfuld, aes(waiting, eruptions)) +
  geom_contour_filled(aes(z = density)) +
  scale_fill_viridis_d(option = "B")
booms 

# ggmagnify() shadow_args
shadow_args <- list(
  colour = alpha("red", 0.8),
  x_offset = 0,
  y_offset = 0,
  sigma = 10)

#
ggm <- ggmagnify(booms,
                 xlim = c(80, 92), ylim = c(4, 4.8),
                 inset_xlim = c(70, 94), inset_ylim = c(1.7, 3.3),
                 shadow = TRUE, shadow_args = shadow_args,
                 colour = "white")
ggm

ggm$inset <- ggm$inset +
  geom_point(data = faithful, color = "red", fill = "white",
             alpha = 0.7, size = 2, shape = "circle filled")

ggm

ggm$plot <- ggm$plot + scale_fill_grey()
## Scale for fill is already present.
## Adding another scale for fill, which will replace the existing scale.
ggm