|
28 | 28 | #' @rdname animation
|
29 | 29 | #' @author Carson Sievert
|
30 | 30 | #' @examples
|
31 |
| -#' |
32 |
| -#' # for more, see https://cpsievert.github.io/plotly_book/key-frame-animations.html |
33 | 31 | #'
|
34 | 32 | #' df <- data.frame(
|
35 | 33 | #' x = c(1, 2, 2, 1, 1, 2),
|
|
46 | 44 | #' animation_opts(transition = 0)
|
47 | 45 | #'
|
48 | 46 | #' # works the same way with ggplotly
|
49 |
| -#' p <- ggplot(txhousing, aes(month, median)) + |
50 |
| -#' geom_line(aes(group = year), alpha = 0.3) + |
51 |
| -#' geom_smooth() + |
52 |
| -#' geom_line(aes(frame = year, ids = month), color = "red") + |
53 |
| -#' facet_wrap(~ city) |
54 |
| -#' |
55 |
| -#' ggplotly(p, width = 1200, height = 900) %>% |
56 |
| -#' animation_opts(1000) |
57 |
| -#' |
58 |
| -#' if (system.file(package = "gapminder") != "") { |
59 |
| -#' |
60 |
| -#' # use the ids attribute to ensure object constancy |
61 |
| -#' p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, |
62 |
| -#' color = continent, frame = year, ids = country)) + |
63 |
| -#' geom_point() + |
64 |
| -#' scale_x_log10() |
65 |
| -#' ggplotly(p) |
66 |
| -#' |
67 |
| -#' p2 <- ggplot(gapminder, aes(gdpPercap, lifeExp)) + |
68 |
| -#' geom_point(aes(size = pop), alpha = 0.5) + |
69 |
| -#' # animations can be specified on the layer level |
70 |
| -#' geom_point(aes(size = pop, frame = year, ids = country), color = "red") + |
71 |
| -#' scale_x_log10() |
72 |
| -#' ggplotly(p2) |
| 47 | +#' if (interactive()) { |
| 48 | +#' p <- ggplot(txhousing, aes(month, median)) + |
| 49 | +#' geom_line(aes(group = year), alpha = 0.3) + |
| 50 | +#' geom_smooth() + |
| 51 | +#' geom_line(aes(frame = year, ids = month), color = "red") + |
| 52 | +#' facet_wrap(~ city) |
| 53 | +#' |
| 54 | +#' ggplotly(p, width = 1200, height = 900) %>% |
| 55 | +#' animation_opts(1000) |
73 | 56 | #' }
|
| 57 | +#' |
| 58 | +#' |
| 59 | +#' #' # for more, see https://cpsievert.github.io/plotly_book/key-frame-animations.html |
74 | 60 | #'
|
75 | 61 | animation_opts <- function(p, frame = 500, transition = frame, easing = "linear",
|
76 | 62 | redraw = FALSE, mode = "immediate") {
|
|
0 commit comments