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 pathtransition_layers.Rmd
113 lines (89 loc) · 3.96 KB
/
transition_layers.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
---
title: "transistion_layers"
author: "Adam Gruer + Saskia Freytag"
date: "22/11/2018"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cache=TRUE)
library(tidyverse)
library(gganimate)
library(emo)
library(fivethirtyeight)
theme_set(theme_minimal())
```
# transistion_layers()
## what do I think it will do?
I think it will add a frame for each geom_ I add to the plot.
# What does the help say
```{r transition_layers help}
?transition_layers
```
> ## Build up a plot, layer by layer
>
> This transition gradually adds layers to the plot in the order they have been defined. By default prior layers are kept for the remainder of the animation, but they can also be set to be removed as the next layer enters.
> ## Usage
> transition_layers(layer_length, transition_length, keep_layers = TRUE,
from_blank = TRUE, layer_names = NULL)
# First attempt
Add three layers: geom_point and geom_histogram and a geom_bar.
It did do as I expected.
I still struggle to understand the layer_length and transition_length beyond them being relative times.
```{r first attempt}
ggplot(mtcars ) +
geom_point(aes(disp, mpg), colour = "purple", size = 3) +
geom_histogram(aes(disp)) +
geom_bar(aes(cyl))+
transition_layers(layer_length = 1, transition_length = 1, keep_layers = TRUE)
```
It is very literal. `ggplot(mtcars)` is a layer, albeit an uninteresting one. However one of the arguments `from_blank` which defaults to TRUE can be changed to FALSE so as to not show this first layer.
Here I show the effect of not showing the first 'blank' layer.
```{r hid blank layer }
ggplot(mtcars ) +
geom_point(aes(disp, mpg), colour = "purple", size = 3) +
geom_histogram(aes(disp)) +
geom_bar(aes(cyl))+
transition_layers(layer_length = 1, transition_length = 1, keep_layers = TRUE, from_blank = FALSE )
```
I can choose not to keep each layer as I animate by setting `keep_layers = FALSE`.
```{r dont keep layers}
ggplot(mtcars ) +
geom_point(aes(disp, mpg), colour = "purple", size = 3) +
geom_histogram(aes(disp)) +
geom_bar(aes(cyl))+
transition_layers(layer_length = 1, transition_length = 1, keep_layers = FALSE, from_blank = FALSE )
```
I can supply layer names to be used as label literals.
```{r supply label literals}
ggplot(mtcars ) +
geom_point(aes(disp, mpg), colour = "purple", size = 3) +
geom_histogram(aes(disp)) +
geom_bar(aes(cyl)) +
labs(title = "{closest_layer}") +
transition_layers(layer_length = 1, transition_length = 1, keep_layers = FALSE, from_blank = FALSE, layer_names = c("geom_point(disp,mpg)",
"geom_histogram(disp)",
"geom_bar(cyl)")
)
```
## Simulate some stuff
In the next bit we simulate a density and show which points we sample. Then we show what distribution they would form. To do this we can set the parameter `keep_layers = TRUE` and pass `layer_length` a vector of times we want to see each layer for.
```{r simulation}
sim <- data.frame(x = rnorm(10000, 0, 1),
sampled = sample(c("TRUE", rep("FALSE", 9)), 10000, replace = TRUE),
index= 1:10000)
sim <- sim %>% mutate(x_bins =cut(x, 30)) %>% mutate(x_bins2 = as.numeric(x_bins))
sim <- sim[order(sim$x_bins2), ]
sim$y <- 1
sim$y <- unlist(lapply(split(sim$y, sim$x_bins2), cumsum))
sim_1 <- sim %>% filter(sampled==TRUE)
sim_1 <- sim_1[order(sim_1$x_bins2), ]
sim_1$y_1 <- 1
sim_1$y_1 <- unlist(lapply(split(sim_1$y_1, sim_1$x_bins2), cumsum))
ggplot(sim, aes(x_bins, y)) + geom_point() +
geom_point(data=sim_1, aes(x_bins, y), col="grey") +
geom_point(data=sim_1, aes(x_bins, y_1), col="hotpink") +
theme(axis.text.x = element_blank()) +
transition_layers(layer_length = c(1, 2, 2), transition_length = 1,
keep_layers = TRUE, from_blank = FALSE,
layer_names = c("Density", rep("Sampled", 2)))
```