简体   繁体   中英

Change speed of R ggplot gganimate when using rmarkdown render

I have some ggplot objects animated via gganimate , and I would like to change the speed of the animation.

Here is a reproducible example from the github repo here: https://github.com/dgrtwo/gganimate

library(gapminder)
library(ggplot2)
theme_set(theme_bw())

p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent, frame = year)) +
    geom_point() +
    scale_x_log10()

library(gganimate)
gganimate(p)

This animation takes about 12 seconds to cycle all the way through. How could I make it cycle through in say 6 seconds?

I have tried setting time to different values, but to no avail, and it is not clear from the help page.

gganimate(p, time = 0.1)

Update

The interval seems to generally work, but still unclear to me how I could make this work within an Rmarkdown report. As an example, if I place the code below in a file called test.R and then run rmarkdown::render('test.R') my animation runs at the default speed instead of the expected increased speed. How would I make this work in the context of rmarkdown::render ? I've been trying various things from looking here: https://github.com/dgrtwo/gganimate/commit/3aa2064cdfff30feb2b00724ad757fd5a5a62621 , but to no avail.

knitr::opts_chunk$set(message = FALSE, warning = FALSE, fig.show = 'animate')

library(gapminder)
library(ggplot2)
theme_set(theme_bw())

p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent, frame = year)) +
    geom_point() +
    scale_x_log10()

library(gganimate)
capture.output(gganimate(p, interval = 0.2))

We need to set interval option:

gganimate(p, interval = 0.2)

From animation package manual, see ani.options :

interval a positive number to set the time interval of the animation (unit in seconds); default to be 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM