This repository has been archived by the owner on May 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
shadow_trail.Rmd
91 lines (70 loc) · 2.45 KB
/
shadow_trail.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
---
title: "shadow_trail"
author: "Danielle Navarro"
date: "26/11/2018"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(e1071)
library(gganimate)
```
This `shadow_trail()` walk through extends the `shadow_wake()` walk through and uses the same animation.
```{r, createdata, cache=TRUE}
ntimes <- 20 # how many time points to run the bridge?
nseries <- 5 # how many time series to generate?
# function to generate the brownian bridges
make_bridges <- function(ntimes, nseries) {
replicate(nseries, c(0,rbridge(frequency = ntimes-1))) %>% as.vector()
}
# construct tibble
tbl <- tibble(
Time = rep(1:ntimes, nseries),
Horizontal = make_bridges(ntimes, nseries),
Vertical = make_bridges(ntimes, nseries),
Series = gl(nseries, ntimes)
)
# construct the base picture
base_pic <- tbl %>%
ggplot(aes(
x = Horizontal,
y = Vertical,
colour = Series)) +
geom_point(
show.legend = FALSE,
size = 5) +
coord_equal() +
xlim(-2,2) +
ylim(-2,2)
# base animation with no shadow
base_anim <- base_pic + transition_time(time = Time)
base_anim %>% animate(type = "cairo")
```
See the other walk through for details.
### Basic use
```{r, trail1, cache=TRUE}
trail1 <- base_anim +
shadow_trail()
trail1 %>% animate(type = "cairo")
```
To make it a little easier to visualise, let's modify the size and transparency of the trail markers:
```{r, trail2, cache=TRUE}
trail2 <- base_anim +
shadow_trail(size = 2, alpha = .2)
trail2 %>% animate(type = "cairo")
```
### Changing the distance
Whereas `shadow_mark()` shows the raw data in each frame in the data (i.e., does not consider interpolated frames, `shadow_trail()` does not privilege those frames that correspond to your data, and instead leaves the trail behind for interpolated frames as well. To show more trail markers, decrease the `distance`:
```{r, trail3, cache=TRUE}
trail3 <- base_anim +
shadow_trail(distance = 0.01, size = 2, alpha = .2)
trail3 %>% animate(type = "cairo")
```
### Changing the number of frames
By default the trail shows all previous trail markers ("crumbs"). You can modify this so that only a fixed number of trail markers are displayed, which makes `shadow_trail()` behave a little more like `shadow_wake()` than `shadow_mark()`:
```{r, trail4, cache=TRUE}
trail4 <- base_anim +
shadow_trail(distance = 0.01, max_frames = 25, size = 2, alpha = .2)
trail4 %>% animate(type = "cairo")
```