-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear vs non-linear animation.R
69 lines (52 loc) · 1.98 KB
/
linear vs non-linear animation.R
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
### Linear
library(tidyverse)
library(gganimate)
x <- seq(from = 0, to =10, by = 0.1)
y <- 10 + 0.2 *x
step <- 1:length(x)
df <- data.frame(x,y, step)
p <- ggplot()+
theme_minimal()+
geom_rect(aes(xmin = 2.5, xmax = 7.5, ymin =10, ymax = Inf), fill = "lightblue", alpha =0.2)+
geom_point(data = df, aes(x=x, y=y), size = 5, colour = "red")+
ggtitle("linear")+
transition_time(step) +
ease_aes('cubic-in-out')+
shadow_wake(wake_length = 0.5, colour = "orange")+
xlab("Explanatory variable (input)")+
ylab("Response variable (output)")+
theme(axis.title = element_text(size = 24, face = 2, colour = "grey"),
plot.title = element_text(size = 24, face = 2))+
theme(axis.title.x = element_text(margin = margin(t= 20)))+
theme(axis.title.x = element_text(margin = margin(r= 20)))+
theme(axis.text = element_blank())+
labs(subtitle = " ")
animate(p, nframes = 64, renderer = gifski_renderer("linear.gif"),
duration = 9)
### Non-linear
x <- seq(from = -10, to = 10, by =0.02)
y <- 3000 + 2*x^3 + 0.3 * x^2 + 1*x
ggplot()+
geom_point(aes(x= x + 100, y=y))+
ggtitle("non-linear")
step <- 1:length(x)
df <- data.frame(x,y, step)
q <- ggplot()+
theme_minimal()+
geom_rect(aes(xmin = -4, xmax = 4, ymin =0, ymax = Inf), fill = "lightblue", alpha =0.2)+
geom_point(data = df, aes(x=x, y=y), size = 5, colour = "red")+
ggtitle("non-linear")+
transition_time(step) +
ease_aes('cubic-in-out')+
shadow_wake(wake_length = 0.5, colour = "orange")+
xlab("Explanatory variable (input)")+
ylab("Response variable (output)")+
theme(axis.title = element_text(size = 24, face = 2, colour = "grey"),
plot.title = element_text(size = 24, face = 2))+
theme(axis.title.x = element_text(margin = margin(t= 20)))+
theme(axis.title.y = element_text(margin = margin(r= 20)))+
theme(axis.text = element_blank())+
theme(aspect.ratio = 1)+
labs(subtitle = " ")
animate(q, nframes = 64, renderer = gifski_renderer("linear.gif"),
duration = 9)