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

## ----setup, include=FALSE-----------------------------------------------------
library(moveEZ)
library(biplotEZ)
library(tibble)
library(scales)
library(gganimate)
library(RColorBrewer)

## -----------------------------------------------------------------------------
data("Africa_climate")
tibble::tibble(Africa_climate)

## ----message=FALSE------------------------------------------------------------
bp <- biplot(Africa_climate, scaled = TRUE) |>
  PCA(group.aes = Africa_climate$Region) |>
  samples(opacity = 0.8, col = scales::hue_pal()(10)) |>
  plot()

## ----warning=FALSE------------------------------------------------------------
bp |> moveplot(time.var = "Year", group.var = "Region",
               hulls = TRUE, move = FALSE)

## ----echo=FALSE, eval=FALSE, warning=FALSE, message=FALSE---------------------
# anim1 <- bp |> moveplot(time.var = "Year", group.var = "Region",
#                         hulls = TRUE, move = TRUE)
# anim_rendered <- animate(anim1, renderer = gifski_renderer(),
#                          nframes = 100, fps = 10,
#                          width = 800, height = 600, res = 150)
# anim_save("vignettes/anim1.gif", animation = anim_rendered)

## ----warning=FALSE------------------------------------------------------------
bp |> moveplot2(time.var = "Year", group.var = "Region",
                hulls = TRUE, move = FALSE)

## ----warning=FALSE------------------------------------------------------------
bp |> moveplot2(time.var = "Year", group.var = "Region",
                hulls = TRUE, move = FALSE,
                align.time = "1950", reflect = "x")

## ----echo=FALSE, eval=FALSE, warning=FALSE, message=FALSE---------------------
# anim2 <- bp |> moveplot2(time.var = "Year", group.var = "Region",
#                          hulls = TRUE, move = TRUE,
#                          align.time = "1950", reflect = "x")
# anim_rendered <- animate(anim2, renderer = gifski_renderer(),
#                          nframes = 100, fps = 10,
#                          width = 800, height = 600, res = 150)
# anim_save("vignettes/anim2.gif", animation = anim_rendered)

## ----warning=FALSE------------------------------------------------------------
bp |> moveplot3(time.var = "Year", group.var = "Region",
                hulls = TRUE, move = FALSE, target = NULL)

## ----echo=FALSE, eval=FALSE, warning=FALSE, message=FALSE---------------------
# anim3 <- bp |> moveplot3(time.var = "Year", group.var = "Region",
#                          hulls = TRUE, move = TRUE, target = NULL)
# anim_rendered <- animate(anim3, renderer = gifski_renderer(),
#                          nframes = 100, fps = 10,
#                          width = 800, height = 600, res = 150)
# anim_save("vignettes/anim3.gif", animation = anim_rendered)

## -----------------------------------------------------------------------------
data("Africa_climate_target")
tibble::tibble(Africa_climate_target)

## ----warning=FALSE------------------------------------------------------------
bp |> moveplot3(time.var = "Year", group.var = "Region",
                hulls = TRUE, move = FALSE,
                target = Africa_climate_target)

## ----warning=FALSE------------------------------------------------------------
viz_1989 <- Africa_climate_target |>
  dplyr::mutate(
    Target = as.factor(rep("1989", nrow(Africa_climate_target))),
    Region = as.factor(Region)
  )

bp_1989 <- biplot(viz_1989, scaled = TRUE) |>
  PCA(group.aes = viz_1989$Region)

bp_1989 |> moveplot(time.var = "Target", group.var = "Region",
                    hulls = TRUE, move = FALSE)

## ----echo=FALSE, eval=FALSE, warning=FALSE, message=FALSE---------------------
# anim4 <- bp |> moveplot3(time.var = "Year", group.var = "Region",
#                          hulls = TRUE, move = TRUE,
#                          target = Africa_climate_target)
# anim_rendered <- animate(anim4, renderer = gifski_renderer(),
#                          nframes = 100, fps = 10,
#                          width = 800, height = 600, res = 150)
# anim_save("vignettes/anim4.gif", animation = anim_rendered)

## ----message=FALSE------------------------------------------------------------
results <- bp |>
  moveplot3(time.var = "Year", group.var = "Region",
            hulls = TRUE, move = FALSE, target = NULL) |>
  evaluation()

## -----------------------------------------------------------------------------
results$eval.list

## ----warning=FALSE------------------------------------------------------------
results$fit.plot

## ----warning=FALSE------------------------------------------------------------
results$bias.plot

## ----warning=FALSE------------------------------------------------------------
bp |> moveplot(time.var = "Region", group.var = "Region",
               hulls = FALSE, move = FALSE)

## ----warning=FALSE, message=FALSE---------------------------------------------
bp_custom <- biplotEZ::biplot(Africa_climate, scaled = TRUE,
                               group.aes = Africa_climate$Region) |>
  biplotEZ::PCA() |>
  biplotEZ::samples(col = RColorBrewer::brewer.pal(10, "Paired")) |>
  biplotEZ::axes(label.cex = 1.2)

bp_custom |> moveplot(time.var = "Year", group.var = "Region",
                      hulls = TRUE, move = FALSE)

## ----warning=FALSE------------------------------------------------------------
bp_pch <- biplotEZ::biplot(Africa_climate, scaled = TRUE,
                            group.aes = Africa_climate$Region) |>
  biplotEZ::PCA() |>
  biplotEZ::samples(pch = c(22, 21, 24, 23), opacity = 0.4)

bp_pch |> moveplot(time.var = "Year", group.var = "Region",
                   hulls = FALSE, move = FALSE)

