Control speed of a gganimation
I found in docs animate
function which can take fps and detail parameters.
@param fps The frame rate of the animation in frames/sec
@param detail The number of additional frames to calculate, per frame
The result:
p <- ggplot(dat, aes(x, y)) +
geom_hex(bins = 5) +
theme_void() +
lims(x = c(.3, .7),
y = c(.3, .7)) +
theme(legend.position = "none") +
transition_time(t_state)
animate(p, fps=1)
Also there you can specify output format such as png, jpeg, svg.
Slow down gganimate in R
a <- df_daily %>%
ggplot(aes(longitude, latitude)) +
geom_point(aes(alpha = a)) +
transition_time(flu_day) +
ease_aes('linear', interval = 0.001) +
labs(title = 'Date: {frame_time}')
animate(a,
duration = 274, # = 365 days/yr x 3 years x 0.25 sec/day = 274 seconds
fps = [pick how smooth you want],
nframes = [...or pick it here])
How to set the speed of the animation in R?
Possible duplicate of: Control speed of a gganimation
In any case, I think this does what you want, if you play around with the fps
argument a litte:
p <- ggplot(dataset, aes(x=Color, y=Weight)) + geom_point()
order <- as.numeric(dataset$Time)
gif <- p +
transition_time(order) +
labs(title = "TIME: {frame_time}") + enter_fade()
animate(gif, fps = 2)
Display different time elements at different speeds in gganimate
This is my rudimentary try by making a helper column which can be used as our transition_time
to show how we can have different time step but desired labels.
You can later spend some more time to make a better *timestep
columns which is more sophisticated and precisely meets your needs.
The main idea/point here is that we can use functions on the frame_time
to get the labels as needed while the transition_time
can be manipulated.
library(ggplot2)
library(gganimate)
library(dplyr)
g <- airquality %>%
group_by(Month) %>%
mutate(timestep = if_else(Month==5, ((1:n())-1)/2 + Month, 15 + Month)) %>%
ggplot(aes(Day, Temp, group = interaction(Month, Day))) +
geom_point(color = 'red', size = 1) +
transition_time(timestep) +
shadow_mark(colour = 'black', size = 0.75) +
enter_fade() +
labs(title = 'Month: {if_else(frame_time<21,5, ceiling(frame_time-15))}')
animate(g, nframes = 100)
Created on 2019-06-02 by the reprex package (v0.3.0)
How to make the transition time between 2 frames longer in gganimate
EDIT: I realized a WAY simpler approach using transition_states
. If we know the number of frames, we can manually specify how much time to spend on each transition, giving it more time at the 2010-2011 step.
my_copious_data <- read_csv("https://raw.githubusercontent.com/Patricklv/Animation-plot-emphasizing-2009-and-2010/main/animationData.csv")
p <- ggplot(data = my_copious_data %>%
filter(year >= 2000),
aes(rate, ridit, group = location_name))+
geom_point() +
transition_states(year, state_length = 0,
transition_length = c(rep(1, 10), 9, rep(1, 9))) +
labs(title = "Year: {as.integer(previous_state)}")
animate(p, nframes = 100, fps = 20, width = 300, height = 300, end_pause = 20)
Prior kludgey answer:
This would be a cool feature have built-in, but is currently "off-label" and I don't know an easy way to do it without some manual construction.
One hacky alternative in this case would be to artificially stretch the timeline so that year 2010 is multiple years long (it runs from 2010 to 2018, with 2011-2019 shifted to 2019-2027), and then "undo" that shift in the titling.
This works with transition_time
because, unlike the other gganimate transition functions, the transition length is scaled to the numerical difference between each state.
my_copious_data <- read_csv("https://raw.githubusercontent.com/Patricklv/Animation-plot-emphasizing-2009-and-2010/main/animationData.csv")
p <- ggplot(data = my_copious_data %>%
filter(year >= 2000) %>%
mutate(year = year + if_else(year > 2010, 8, 0)),
aes(rate, ridit, group = location_name))+
geom_point() +
transition_time(year) +
labs(title = "Year: {as.integer(frame_time) - pmax(0, pmin(8, as.integer(frame_time) - 2010))}")
animate(p, nframes = 100, fps = 20, end_pause = 20, width = 300, height = 300)
[SO limits uploaded video to 2MB, so you may be able to use a higher resolution, higher frame count animation in your context.]
Here's a little explanation about the shifting.
my_copious_data %>%
filter(year >= 2000, location_name == "California") %>%
mutate(year_stretched = year + if_else(year > 2010, 8, 0),
year_back_to_normal = year_stretched - pmax(0, pmin(8, year_stretched - 2010))) -> example
# A tibble: 20 x 6
location_name year rate ridit year_stretched year_back_to_normal
<chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 California 2000 22938. 0.488 2000 2000
2 California 2001 22253. 0.486 2001 2001
3 California 2002 20974. 0.485 2002 2002
4 California 2003 21969. 0.483 2003 2003
5 California 2004 19669. 0.481 2004 2004
6 California 2005 19829. 0.480 2005 2005
7 California 2006 19662. 0.462 2006 2006
8 California 2007 19835. 0.461 2007 2007
9 California 2008 20270. 0.457 2008 2008
10 California 2009 19867. 0.452 2009 2009
11 California 2010 19734. 0.406 2010 2010
12 California 2011 18007. 0.400 2019 2011
13 California 2012 20611. 0.399 2020 2012
14 California 2013 23602. 0.398 2021 2013
15 California 2014 19733. 0.397 2022 2014
16 California 2015 19472. 0.396 2023 2015
17 California 2016 19561. 0.395 2024 2016
18 California 2017 17764. 0.394 2025 2017
19 California 2018 20922. 0.393 2026 2018
20 California 2019 21015. 0.393 2027 2019
When the year is higher than 2010, we add 8 (arbitrarily) to it, which makes the transition from 2010 to 2011 take 9 years of animation time instead of 1 year like normal. But if we left it at that our years would be reported incorrectly. So I add a reversing transformation that takes the "animation time" and puts it back into real-world time. This is done by taking the difference between animation time and 2010 [year_stretched - 2010
] and capping that between 0 and 8 using pmin
and pmax
.
ggplot(example, aes(year, year_stretched)) +
geom_point() +
geom_line()
Define size for .gif created by gganimate - change dimension / resolution
There can be issues with using the magick
package.
I think a better solution is use the animate()
function in gganimate
to create an object which is then passed to the anim_save()
function. No need to use another package.
library(gganimate)
library(gapminder)
my.animation <-
ggplot(
gapminder,
aes(x = gdpPercap, y = lifeExp, colour = continent, size = pop)
) +
geom_point(alpha = 0.6) +
scale_x_log10() +
transition_time(year)
# animate in a two step process:
animate(my.animation, height = 800, width =800)
anim_save("Gapminder_example.gif")
Related Topics
Accessing Parent Namespace Inside a Shiny Module
Joining Factor Levels of Two Columns
Extract Hyperlink from Excel File in R
Create a New Variable Based on the First 7 Characters of Existing Variable
How to Replace Multiple Strings with the Same in R
Filling in a New Column Based on a Condition in a Data Frame
Predict X Values from Simple Fitting and Annoting It in the Plot
Topic Models: Cross Validation with Loglikelihood or Perplexity
Plot a Character Vector Against a Numeric Vector in R
How to Merge Two Nodes into a Single Node Using Igraph
Vary the Color Gradient on a Scatter Plot Created with Ggplot2
R Group By, Counting Non-Na Values
Sum Specific Columns Among Rows
How to Read a Text File into Gnu R with a Multiple-Byte Separator
Sample Function Gives Different Result in Console and in Knitted Document When Seed Is Set