Search code examples
ranimationffmpeggganimate

Why can I not change the number of frames (nframes) in a gganimate animation? SOLVED


I have produced an animation per gganimate and rendered it per ffmpeg. It works just fine, but only, if I do not change the number of frames. If I do set the number of frames, I get this error message:

nframes and fps adjusted to match transition
Error parsing framerate 8,4.                           
Error: Rendering with ffmpeg failed

I produced the gganim MonthlyAveragePrecipitationMap the following way:

options(scipen = 999, OutDec  =  ",")

MonthlyAveragePrecipitationMap = ggplot(MonthlyAverageExtremePrecipitation) + 
  geom_path(data = map_data("world","Germany"),
            aes(x = long, y = lat, group = group)) +
  coord_fixed(xlim = c(6,15),
              ylim = c(47,55)) + 
  geom_point(aes(x=lon, y=lat, 
                 colour = ShareOfExtremePrecipitationEvents,
                 group = MonthOfYear),
             size = 3) + 
  scale_color_gradient(low="blue", high="yellow") + 
  xlab("Longitude (degree)") +
  ylab("Latitude (degree)") + 
  theme_bw() +
  transition_manual(frames = MonthOfYear) + 
  labs(title = '{unique(MonthlyAverageExtremePrecipitation$MonthOfYear)[as.integer(frame)]}', 
       color = paste0("Share of Extreme Precipitation Events \namong all Precipitation Events")) 

I call the animation the following way:

animate(MonthlyAveragePrecipitationMap,
        nframes = 300,
        renderer =
          ffmpeg_renderer(
            format = "auto",
            ffmpeg = NULL,
            options = list(pix_fmt = "yuv420p")))

I used this exact code just a few days ago and it worked fine.

Has someone had similar experiences? Thanks in advance.

EDIT: Problem solved.

  • Problem: Changing the decimal seperator from . to , per options(dec=",")
  • Solution: Changing the decimal seperator locally within the axis scaling per function.

Solution

  • I assume that your problem is that ffmpeg, which you use to render the frames into an animation, is a separate command line tool which does not understand that the , indicates the decimal separator. Since you probably only want to format the numbers in your plot, the cleaner way to do that would be to change the labels directly. A small example:

    library(tidyverse)
    library(gganimate)
    comma_decimal <- function(x) {
      gsub(".", ",", x, fixed = TRUE)
    }
    
    ggplot(mtcars, aes(wt, mpg)) +
      geom_point() +
      scale_x_continuous(labels = comma_decimal, n.breaks = 15)
    

    Created on 2022-12-26 with reprex v2.0.2

    This way, the separator change does not interfere with the rest of your code.