3

I am making plots using ggplot2+gganimate+ggflags, but it renders very slow: typically at or less than 0.1 frames per second. So if I have, say, 300 frames, it takes a very long time. I have noticed that it is ggflags that causes it to become much slower than otherwise. Is there anything I can do to speed up the rendering? I am using a MacBook Pro.

My very basic code setup looks something like:

plot <- data %>% ggplot2(aes(...)) + geom_flag(...) + lots of options + transition_time(year) 

anim_save("file.gif", plot, ...)
bill999
  • 2,147
  • 8
  • 51
  • 103

1 Answers1

6

It's hard to answer definitively without a specific example.

When I run the following, it produces about 3.5 fps on my 2012 macbook air. With geom_point in place of geom_flag, it produces about 4.5 fps. This doesn't strike me as a particularly big slowdown given that the flags are more complicated to draw. Do you have other options, or very many flags, that might be making it slower?

library(tidyverse)
library(ggflags)  # Using v0.0.1 downloaded 11/30/19 from 
                  #    https://github.com/ellisp/ggflags  
library(gganimate)
library(gapminder)

animate(
  gapminder %>% 
  filter(continent == "Asia") %>%
  ggplot(aes(gdpPercap, lifeExp, country = country)) + 
  geom_flag() +
  transition_time(year),
  width = 700, height = 400, fps = 20)

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • 2
    what's the red-and-yellow flag with the crazy dynamics? – Ben Bolker Dec 01 '19 at 00:17
  • 1
    Kuwait: `library(plotly); gg0 <- gapminder %>% filter(continent=="Asia") %>% ggplot(aes(x=year,y=gdpPercap,colour=country))+geom_line(); ggplotly(gg0)` – Ben Bolker Dec 01 '19 at 00:20
  • Thanks. As it happens, my real application is very similar to your example. I tried running your code as is. One difference is that my ggflags appears to be different in that mine uses circle flags (see https://github.com/rensa/ggflags). Another is that I am using `anim_save` instead of `animate`. I also have more countries and more years. But running the code as you have it (with the different ggflags), I still get a very slow results: 0.43 fps. If I get rid of the Asia restriction, this drops down to 0.12 frames per second. – bill999 Dec 03 '19 at 02:26
  • I also tried geom_point (still with no Asia restriction) instead of ggflags, and I get an order of magnitude faster and in line with your number—4.2 fps, so the issue appears to be with the rensa version of ggflags. I also note that I am using a 2010 MacBook Pro. – bill999 Dec 03 '19 at 02:26
  • 2
    It looks like the rensa version of ggflags uses `grImport2` to load and render an SVG image; the original uses `grid::raster()`. I can believe that makes the difference. – Ben Bolker Dec 03 '19 at 14:32
  • @BenBolker, makes sense. Thanks! – bill999 Dec 03 '19 at 17:02
  • 1
    maybe @JonSpring (or someone) could do a side-by-side comparison of the two packages ... – Ben Bolker Dec 03 '19 at 17:13