---
title: "moveEZ"
output:
rmarkdown::html_vignette:
toc: true
number_sections: true
vignette: >
%\VignetteIndexEntry{moveEZ}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
fig.height = 6, fig.width = 7,
collapse = TRUE,
comment = "#>"
)
```
```{r setup, include=FALSE}
library(moveEZ) ; library(tibble) ; library(scales); library(gganimate)
```
Consider a dataset ${\bf{X}}$ comprising $n$ observations and $p$ continuous variables, along with an additional variable representing "time". This time variable need not correspond to chronological time; it could just as well represent another form of ordered index, such as algorithmic iterations or experimental stages.
A natural approach is to construct separate biplots for each level of the time variable, enabling the user to explore how samples and variable relationships evolve across time. However, when the time variable includes many levels, this quickly results in an overwhelming number of biplots.
This package addresses that challenge by animating a single biplot across the levels of the time variable, allowing for dynamic visualisation of temporal or sequential changes in the data.
The animation of the biplots—currently limited to PCA biplots—is based on two conceptual frameworks:
1. Fixed Variable Frame `moveplot()`: A biplot is first constructed using the full dataset ${\bf{X}}$, and the animation is achieved by slicing the observations according to the "time" variable. In this approach, the variable axes remain fixed, and only the sample points are animated over time.
2. Dynamic Frame `moveplot2()` and `moveplot3()`: Separate biplots are constructed for each time slice of the data. Both the sample points and variable axes evolve over time, resulting in a fully dynamic animation that reflects temporal changes in the underlying data structure. The differences between these functions are highlighted in the subsequent sections.
To illustrate the animated biplots, we use a climate dataset included in the package. This dataset, Africa_climate, contains climate measurements from 10 African regions over time:
```{r}
library(moveEZ)
data("Africa_climate")
tibble::tibble(Africa_climate)
```
We begin by constructing a standard PCA biplot using the `biplotEZ` package. This biplot aggregates all samples across time and colours them according to their associated region:
```{r, message=FALSE}
library(biplotEZ)
bp <- biplot(Africa_climate, scaled = TRUE) |>
PCA(group.aes = Africa_climate$Region) |>
samples(opacity = 0.8, col = scales::hue_pal()(10)) |>
plot()
```
# Fixed Variable Frame with `moveplot()`
Using the previously created PCA biplot object `bp`, the `moveplot()` function enables animation of the sample points over time. This function is piped with several key arguments:
- `time.var`: Specifies the name of the variable in the dataset that represents the temporal or sequential dimension. In this case, the variable "Year" relates to the time variable.
- `group.var`: Indicates a grouping variable used for colour-coding. In this case, the variable "Region" relates to the group variable.
- `hulls`: A logical flag that determines whether to display individual sample points or to draw convex hulls around each group.
`move`: A critical argument that controls whether the biplot is animated. If set to `TRUE`, the sample points are animated across time. If set to `FALSE`, the function returns a faceted plot showing a static biplot for each time level.
This design provides flexibility in exploring temporal dynamics in multivariate data, with options for both animated and comparative static visualisations.
## Facet: `move = FALSE`
```{r,warning=FALSE}
bp |> moveplot(time.var = "Year", group.var = "Region", hulls = TRUE, move = FALSE)
```
## Animation: `move = TRUE`
```{r,warning=FALSE, message=FALSE}
bp |> moveplot(time.var = "Year", group.var = "Region", hulls = TRUE, move = TRUE)
```
# Dynamic Frame `moveplot2()`
The `moveplot2()` function extends the animation to both the sample points and the variable axes. Unlike `moveplot()`, which keeps the variable axes fixed, `moveplot2()` constructs a separate biplot for each time slice, allowing both components to evolve over time. The function shares the same arguments as `moveplot()`, with the `move` argument determining whether the animation is shown or presented as static facets for samples and variables.
## Facet: `move = FALSE`
```{r,warning=FALSE}
bp |> moveplot2(time.var = "Year", group.var = "Region", hulls = TRUE, move = FALSE)
```
When `move` is `FALSE`, a faceted plot is returned, showing the biplot at each time point. Here, both the sample coordinates and variable axes differ across facets, reflecting temporal changes in the data structure.
There is a noticeable discontinuity in the transition from the year 1950 to 1960. From 1960 onwards, however, the biplots appear well-aligned. To address such inconsistencies, the `moveplot2()` function provides two additional arguments — `align.time` and `reflect` — which enable alignment and optional axis reflections of the biplots at specified time points, resulting in smoother and more coherent animations.
## Animated: `move = TRUE`
```{r,warning=FALSE, message=FALSE}
bp |> moveplot2(time.var = "Year", group.var = "Region", hulls = TRUE, move = TRUE,
align.time = "1950", reflect = "x")
```
Setting `move` to TRUE produces an animated biplot in which both the samples and variables transition across time, offering a dynamic view of structural shifts in the multivariate space.
In the example above, we align the biplot at the 1950 time point and apply a reflection about the x-axis. Available options include:
- "x" – Reflect about the x-axis
- "y" – Reflect about the y-axis
- "xy" – Reflect about both axes
And of course, both `align.time` and `reflect` can be vectors when alignment is needed at multiple time points. Each entry in `reflect` corresponds to a time point in `align.time`, allowing fine-grained control over the alignment and orientation of biplots across the animation sequence.
# Dynamic frame with alignment to a `target` with `moveplot3()`
This function shares the same arguments as `moveplot()` and `moveplot2()`, with the addition of the `target` argument. `moveplot3()` utilises Generalised Orthogonal Procrustes Analysis (GPA) to align sample points and variable axes to either a specified target (for example: same measurements at a different time point) or to a centroid coordinate matrix representing all sample points and axes across time slices (`target = NULL`). GPA is applied by using the `GPAbin` package and makes use of admissible transformations (translation, scaling, rotation and reflection) to optimally align configurations, while preserving the distances between coordinates. As with `moveplot2()` the `move` argument determines whether the animations of changing sample points and variables axes are shown or presented as static facets.
To illustrate the use of a fixed target, we use the year 1989 from the `Africa_climate` data set, which consists of the same variables and number of observations:
```{r}
data("Africa_climate_target")
tibble::tibble(Africa_climate_target)
```
## Facet: `move = FALSE` and `target = NULL`
```{r,warning=FALSE}
bp |> moveplot3(time.var = "Year", group.var = "Region", hulls = TRUE, move = FALSE,
target = NULL)
```
The separate biplots per `time.var` are transformed and aligned to the centroid coordinate matrix of all observed sample points and axes variables.
## Facet: `move = FALSE` and `target = Africa_climate_target`
```{r,warning=FALSE}
bp |> moveplot3(time.var = "Year", group.var = "Region", hulls = TRUE, move = FALSE,
target = Africa_climate_target)
```
Now, the separate biplots per `time.var` are transformed and aligned to the sample points and axes variables of the 1989 `Africa_climate` dataset. **Take note**: the target biplot is not shown. This example showcases the difference between each the observations and variables for each year in `Africa_climate` compared to 1989.
## Animated: `move = TRUE` and `target = NULL`
```{r,warning=FALSE, message=FALSE}
bp |> moveplot3(time.var = "Year", group.var = "Region", hulls = TRUE, move = TRUE,
target = NULL)
```
Here the animated view of the biplots over time are illustrated after aligning the visualisation to the centroid configuration.
## Animated: `move = TRUE` and `target = Africa_climate_target`
```{r,warning=FALSE, message=FALSE}
bp |> moveplot3(time.var = "Year", group.var = "Region", hulls = TRUE, move = TRUE,
target = Africa_climate_target)
```
Finally, the animated biplots illustrate the transformations towards a specified target dataset. Again, the focus is on the movement that changes between the variables and sample representation as the target is set to a specific year compared to the movement observed in the previous example where `target = NULL`. Therefore, these animations expose the *jumps* that occur from 1989 to each of the years in `Africa_climate` from 1950 to 2020 (in increments of 10 years).
# Still to Come!
We are actively working to develop and enhance the dynamic plotting capabilities of these functions to expose and detect changes in observations and variables over time.
Stay tuned for updates!