This repository was 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
/
Copy pathshadow_mark.Rmd
141 lines (114 loc) · 3.67 KB
/
shadow_mark.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
---
title: "shadow_mark"
author: "Danielle Navarro & Dale Maschette"
date: "22/11/2018"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(e1071)
library(gganimate)
```
This `shadow_mark()` 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(-1.5, 1.5) +
ylim(-1.5, 1.5)
# base animation with no shadow
base_anim <- base_pic + transition_time(time = Time)
base_anim %>% animate()
```
See the other walk through for details.
## Minimal use of shadow mark
```{r, mark0, cache = TRUE}
mark0 <- base_anim + shadow_mark()
mark0 %>% animate(type = "cairo")
```
## Setting fixed aesthetics
```{r, mark1, cache = TRUE}
mark1<- base_anim +
shadow_mark(
size = 2,
alpha = 0.8,
colour = "black"
)
mark1 %>% animate(type = "cairo")
```
## Excluding layers
Suppose we want to solve Dale's problem in a much simpler way: keep the colours on the shadow mark, but have the original dots all be black. Solution is to add another layer but exclude it from the shadow mark:
```{r, mark2, cache = TRUE}
new_pic <- base_pic +
geom_point(colour = "black", size = 5)
mark2 <- new_pic +
transition_time(time = Time) +
shadow_mark(size = 2, exclude_layer = 2)
mark2 %>% animate(type = "cairo")
```
In some cases the shadow mark might need to rendered using a different colour palette than the main points. This is slightly trickier as `ggplot2` isn't really designed to allow different layers to use different palettes. We can work around it using `scale_colour_manual()` like so:
```{r, mark3, cache = TRUE}
# add a dummy variable Series2
tbl <- tbl %>%
mutate(Series2 = as.factor(as.numeric(Series) + nseries))
# Create a colour map that attaches a colour to each
# named value in Series or Series2. For simplicity they're both
# rainbow() maps, one highly desaturated and the other one not
cmap <- c(rainbow(nseries, s = .4), rainbow(nseries, s = .8))
names(cmap) <- 1:(2*nseries)
# Vonstruct the plot with two geom_point layers
twin_pic <- tbl %>%
ggplot(aes(
x = Horizontal,
y = Vertical,
colour = Series)) +
geom_point( # layer 1
show.legend = FALSE,
size = 5) +
geom_point( # layer 2
mapping = aes(colour = Series2),
show.legend = FALSE,
size = 5
) +
scale_color_manual(values = cmap) +
coord_equal() +
xlim(-1.5, 1.5) +
ylim(-1.5, 1.5)
# Animation with shadow mark, excluding layer 2
mark3 <- twin_pic +
transition_time(time = Time) +
shadow_mark(size = 2, exclude_layer = 2)
mark3 %>% animate(type = "cairo")
```
# Shadow marks from the future
By default `shadow_mark()` displays marks from earlier states but not future state, but this is flexible. The default parameters `past = TRUE` and `future = FALSE` can both be modified. For example, to show the shadow mark only for future points:
```{r, mark4, cache=TRUE}
mark4 <- base_anim +
shadow_mark(
past = FALSE,
future = TRUE,
colour = "grey70"
)
mark4 %>% animate(type = "cairo")
```