27

I'm using gganimate to create some .gif files that I want to insert into my reports. I'm able to save the files and view them fine, however, I find that the displayed size is small: 480x480. Is there a way to adjust that - perhaps along the lines of height and width arguments in ggsave()?

I can zoom in but that impacts the quality poorly and makes it rather unreadable for my use case.

Here's some sample code:

gplot <- 
  ggplot(gapminder, 
         aes(x = gdpPercap, y = lifeExp, colour = continent, 
             size = pop, 
             frame = year)) +
    geom_point(alpha = 0.6) + 
    scale_x_log10()

gganimate(gplot, "test.gif")

Below is the output for this code.

test.gif

tjebo
  • 21,977
  • 7
  • 58
  • 94
Gautam
  • 2,597
  • 1
  • 28
  • 51
  • I think you want `ani.options(ani.width=800, ani.height=400)` or set those to whatever you want. – MrFlick Mar 01 '18 at 21:03
  • @Tjebo thanks for bringing this up, I'll take a look and change the accepted answer if another option works better. – Gautam May 14 '20 at 03:48

4 Answers4

39

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")
Nathan
  • 646
  • 5
  • 10
23

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. enter image description here

tjebo
  • 21,977
  • 7
  • 58
  • 94
  • 2
    update, a pull request for updating the documentation was accepted, so this should now be available at least on the development version of gganimate – tjebo May 14 '20 at 12:28
  • 1
    This should be the top answer. – Steen Harsted Jun 04 '20 at 09:43
  • Your answer says "res=150" in `animate`. The docs for `animate` have no description of "res", see https://gganimate.com/reference/animate.html. Can you link a reference that describes res? – dfrankow Jun 16 '21 at 13:48
  • @dfrankow this is all explained in my answer already... see _?animate shows that device arguments can be specified via the ... parameter. You can find available arguments at ?grDevices::png or ?grDevices::svg._ a better name for ... might be argument. I will change that. – tjebo Jun 16 '21 at 18:48
  • 1
    I see, thank you! I did not understand that `res` was an argument to devices (i.e., linking to the preceding paragraph). Indeed when I look at `?grDevices::png`, it describes `res` precisely. – dfrankow Jun 16 '21 at 21:31
10

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"
)
spren9er
  • 744
  • 8
  • 11
3

You can either tune the general settings:

animation::ani.options(ani.width= 1000, ani.height=1000, ani.res = 1000)

or change the settings of each single command:

gganimate(gplot, ani.width= 1000, ani.height=1000, "test.gif")
oibaFox
  • 75
  • 6