I'd like to create a gif using gganimate
, but my axis ranges vary wildly in one frame. This is causing all subsequent frames to be squeezed.
In ggplot2
's facets, there's an option to have scales="free"
. Is there a way to have free scales in each frame of gganimate
?
Here's an example:
library(gapminder)
library(ggplot2)
library(gganimate)
theme_set(theme_bw())
p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent,
frame = year)) +
geom_point() +
scale_x_log10()
gganimate(p)
Now we move one of the data points to some extreme value. This squeezes the points in all subsequent unaffected frames.
gapminder[1, "lifeExp"] <- 1000
gapminder[1, "gdpPercap"] <- 1e60
p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent,
frame = year)) +
geom_point() +
scale_x_log10()
gganimate(p) # smooshed
You can try experimenting with view_follow()
.
p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent)) +
geom_point() +
labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
transition_time(year) +
view_follow()
animate(p)
To manually define the scales see view_step
and view_step_manual
(also view_zoom
and view_zoom_manual
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With