-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmygg_animate.R
More file actions
58 lines (57 loc) · 1.78 KB
/
mygg_animate.R
File metadata and controls
58 lines (57 loc) · 1.78 KB
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
mygg_animate <- function (p = last_plot(), filename = NULL, saver = NULL, title_frame = TRUE,
...)
{
if (is.null(p)) {
stop("no plot to save")
}
built <- ggplot_build(p)
frames <- plyr::compact(lapply(built$data, function(d) d$frame))
titles <- plyr::compact(lapply(built$data, function(d) d$ttl))
if (length(frames) == 0) {
stop("No frame aesthetic found; cannot create animation")
}
# if (is.factor(frames[[1]])) {
frames.unique <- sort(unique(unlist(frames)))
# }
# else {
# frames.unique <- sort(unique(do.call(c, frames)))
# }
frames.unique <- sort(unique(frames.unique))
plots <- lapply(1:length(frames.unique), function(k) {
f <- frames.unique[k]
t <- titles[[1]][which(frames[[1]]==f)[1]]
b <- built
for (i in seq_along(b$data)) {
frame_vec <- b$data[[i]]$frame
if (!is.null(frame_vec)) {
sub <- (frame_vec == f | is.na(frame_vec))
if (!is.null(b$data[[i]]$cumulative)) {
sub <- sub | (b$data[[i]]$cumulative & (frame_vec <=
f))
}
b$data[[i]] <- b$data[[i]][sub, ]
}
}
if (title_frame) {
if (!is.null(b$plot$labels$title)) {
b$plot$labels$title <- paste(b$plot$labels$title,
t)
}
else {
b$plot$labels$title <- t
}
}
b
})
ret <- list(plots = plots, frames = frames.unique)
class(ret) <- "gg_animate"
if (!is.null(filename)) {
gganimate:::gganimate_save(ret, filename, saver, ...)
ret$saved <- TRUE
}
else {
ret$ani_opts <- list(...)
ret$saved <- FALSE
}
ret
}