-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.R
61 lines (55 loc) · 2.68 KB
/
script.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
###########################################################################
###########################################################################
### ###
### ANIMATION PLOT WITH GGANIMATE ###
### ###
###########################################################################
###########################################################################
##----------------------------------------------------------------
## Load the library --
##----------------------------------------------------------------
library(tidyverse) # data wrangling
library(gganimate) # data visualization
library(ggthemes) # set plot theme
library(gapminder) # the dataset
##---------------------------------------------------------------
## EDA --
##---------------------------------------------------------------
data <- gapminder
glimpse(data)
##---------------------------------------------------------------
## Creating basic static plot --
##---------------------------------------------------------------
# Make a ggplot, but add frame=year: one image per year
graph1 <- gapminder %>%
# plot the data with scatterplots
ggplot(aes(gdpPercap, lifeExp, size = pop, color = continent)) +
geom_point() +
# axis transformations
scale_x_log10() +
theme_bw()
graph1
##---------------------------------------------------------------
## Transition through distinct states in time --
##---------------------------------------------------------------
graph1_animation = graph1 +
transition_time(year) +
labs(subtitle = "Year: {frame_time}")
graph1_animation
##----------------------------------------------------------------
## Display preceding frames with a gradual decrease in size --
##----------------------------------------------------------------
graph1_animation <- graph1 +
transition_time(year) +
labs(subtitle = "Year: {frame_time}") +
shadow_wake(wake_length = 0.1, alpha = FALSE)
graph1_animation
##---------------------------------------------------------------
## Make the label more visible --
##---------------------------------------------------------------
graph1_animation_2 <- graph1 +
geom_text(aes(x = min(gdpPercap), y = min(lifeExp), label = as.factor(year)) ,
hjust=-1.5, vjust = -0.2, alpha = 0.2, col = "gray", size = 20) +
transition_states(as.factor(year), state_length = 0)
#display the graph
graph1_animation_2