Define size for .gif created by gganimate - change dimension / resolution
Using the new API of gganimate
package, it is
library(gganimate)
library(gapminder)
gplot <-
ggplot(
gapminder,
aes(x = gdpPercap, y = lifeExp, colour = continent, size = pop)
) +
geom_point(alpha = 0.6) +
scale_x_log10() +
transition_time(year)
magick::image_write(
animate(gplot, width = 1000, height = 1000),
"test.gif"
)
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")
Although Thomas suggests to have a look at animate
, the documentation is unfortunately not very clear in this regard.
?animate
shows that device arguments can be specified via the ...
argument. You can find available arguments at ?grDevices::png
or ?grDevices::svg
.
You can directly control the resolution by specifying the res
argument. And one can also use different units. I personally like to control my figure dimensions in inch and control the resolution based on that.
The advantage is to me, that there will be much less surprises with font sizes and of course the quality of the figure will be better.
Based on the example provided by user Nathan.
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) +
theme_bw(base_size = 8)
animate(my.animation, height = 2,
width = 3, units = "in", res = 150)
anim_save("gapminder_example.gif")
dimensions of 450x300px, as expected.