File: gganimate.R

package info (click to toggle)
r-cran-gganimate 1.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,876 kB
  • sloc: sh: 13; makefile: 2
file content (100 lines) | stat: -rw-r--r-- 3,252 bytes parent folder | download
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
## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  gganimate = list(
    nframes = 50,
    width = 1000,
    height = 650,
    units = "px",
    res = 144
  ),
  out.width = '100%'
)

## -----------------------------------------------------------------------------
library(gganimate)

# We'll start with a static plot
p <- ggplot(iris, aes(x = Petal.Width, y = Petal.Length)) + 
  geom_point()

plot(p)

## -----------------------------------------------------------------------------
anim <- p + 
  transition_states(Species,
                    transition_length = 2,
                    state_length = 1)

anim

## -----------------------------------------------------------------------------
anim + 
  ease_aes('cubic-in-out') # Slow start and end for a smoother look

## -----------------------------------------------------------------------------
anim + 
  ease_aes(y = 'bounce-out') # Sets special ease for y aesthetic

## -----------------------------------------------------------------------------
anim + 
  ggtitle('Now showing {closest_state}',
          subtitle = 'Frame {frame} of {nframes}')

## -----------------------------------------------------------------------------
ggplot(iris, aes(x = Petal.Width, y = Petal.Length)) + 
  geom_line(aes(group = rep(1:50, 3)), colour = 'grey') + 
  geom_point()

## -----------------------------------------------------------------------------
ggplot(iris, aes(x = Petal.Width, y = Petal.Length)) + 
  geom_point(aes(colour = Species)) + 
  transition_states(Species,
                    transition_length = 2,
                    state_length = 1)

## -----------------------------------------------------------------------------
ggplot(iris, aes(x = Petal.Width, y = Petal.Length)) + 
  geom_point(aes(group = seq_along(Species))) + 
  transition_states(Species,
                    transition_length = 2,
                    state_length = 1)

## -----------------------------------------------------------------------------
ggplot(iris, aes(x = Petal.Width, y = Petal.Length)) + 
  geom_point(aes(colour = Species, group = 1L)) + 
  transition_states(Species,
                    transition_length = 2,
                    state_length = 1)

## -----------------------------------------------------------------------------
anim <- ggplot(iris, aes(x = Petal.Width, y = Petal.Length)) + 
  geom_point(aes(colour = Species), size = 2) + 
  transition_states(Species,
                    transition_length = 2,
                    state_length = 1)

anim + 
  enter_fade() + 
  exit_shrink()

## -----------------------------------------------------------------------------
anim + 
  enter_fade() + enter_drift(x_mod = -1) + 
  exit_shrink() + exit_drift(x_mod = 5)

## ----eval=requireNamespace('av', quietly = TRUE)------------------------------
#  # Video output
#  animate(
#    anim + enter_fade() + exit_fly(y_loc = 1),
#    renderer = av_renderer()
#  )

## ----out.width=NULL-----------------------------------------------------------
# Different size and resolution
animate(
  anim + ease_aes(x = 'bounce-out') + enter_fly(x_loc = -1) + exit_fade(),
  width = 400, height = 600, res = 35
)