将两个动画情节组合成一个GIF / MP4

时间:2019-05-03 10:24:13

标签: r ggplot2 gganimate

我已经基于同一数据集创建了两个动画时间序列图。一个是显示总体趋势的折线图,另一个是显示前5个子组的条形图。我想看看是否可以将这些动画组合成一个时间序列动画,并排或以折线图作为小插图。

我已经构建了单独的动画,但是我不清楚是否可以将它们组合在一起,或者是否需要在每个帧都有插图的情况下创建一个新图像,然后对其进行动画处理。

这是一个最小的示例数据集:

data <- data.frame(
  Year = c(rep(1988, 5), rep(1989, 5), rep(1990, 5)),
  Total = c(rep(1000, 5), rep(1100, 5), rep(1200, 5),
  SubGroup = c("A", "B", "C", "D", "E", "B", "A", "C", "D", E", "B", "C", "A", "E", "D"),
  Amount = c(200, 180, 100, 80, 60, 210, 200, 150, 100, 80, 250, 240, 200, 150, 110)
)

这是我的前5个条形图动画的代码:

# generate top 5 ranking by year

anim_table <- data %>%
  dplyr::group_by(Year) %>%
  dplyr::mutate(
    rank = min_rank(-Amount) * 1,
    Value_rel = Amount / Amount[rank == 1],
    Value_lbl = paste0(" ", Amount)
  ) %>%
  dplyr::filter(rank <= 5) %>%
  dplyr::ungroup() %>% 
  dplyr::arrange(Year, rank)

# create static barchart

p1 <- ggplot2::ggplot(anim_table, aes(rank)) +
  ggplot2::geom_tile(aes(
    y = Amount / 2,
    height = Amount,
    width = 0.9,
    fill = "blue"
  ), alpha = 0.8, color = NA) +
  ggplot2::geom_text(aes(y = 0, label = paste(SubGroup, " ")), size = 12, vjust = 0.2, hjust = 1) +
  ggplot2::geom_text(aes(y = Amount, label = Value_lbl, hjust = 0)) +
  ggplot2::coord_flip(clip = "off", expand = FALSE) +
  ggplot2::scale_y_continuous(labels = scales::comma) +
  ggplot2::scale_x_reverse() +
  ggplot2::guides(color = FALSE, fill = FALSE) +
  ggplot2::labs(
    title = "{closest_state}", x = "", y = "Amount",
    caption = "Source:  whatever"
  ) +
  ggplot2::theme(
    plot.title = element_text(color = "darkblue", face = "bold", hjust = 0, size = 30),
    axis.ticks.y = element_blank(),
    axis.text.y = element_blank(),
    plot.margin = margin(2, 2, 1, 16, "cm")
  ) +
# animate over Years
  gganimate::transition_states(Year, transition_length = 4, state_length = 1) +
  gganimate::ease_aes("cubic-in") 

gganimate::animate(p1, 200, fps = 5, duration = 100, width = 2000, height = 1200, renderer = gifski_renderer("anim1.gif"))

这是我的简单动画折线图的代码:


p2 <- ggplot2::ggplot(
  hc_total,
  aes(Year, Total)
) +
  ggplot2::geom_line() +
  ggplot2::scale_color_viridis_d() +
  ggplot2::labs(x = "Year", y = "Total") +
  gganimate::transition_reveal(Year)

gganimate::animate(p2, 200, fps = 5, duration = 100, width = 2000, height = 1200, renderer = gifski_renderer("anim2.gif"))

希望弄清楚如何将这两个动画并排或以折线图作为插图组合成一个动画。

1 个答案:

答案 0 :(得分:2)

您可以使用ImageMagick做到这一点。这两个GIF必须具有相同的帧数,并且最好具有相同的大小。

对于Windows,我编写了运行ImageMagick的R函数:

appendGIFs <- function(gif1, gif2, gifout, vertically=FALSE, 
                        delay=20, convert = "C:/PortableApps/ImageMagick/convert"){
  command <- sprintf("%s ( %s -coalesce -set page %s+0+0 -coalesce ) null: ( %s -coalesce ) -gravity %s -layers composite -set delay %d -loop 0 %s", 
                     convert, gif1, ifelse(vertically, "%[w]x%[fx:h*2]", "%[fx:w*2]x%[h]"), 
                     gif2, ifelse(vertically, "south", "east"),  delay, gifout)
  system(command) 
}

在这里,gif1是第一个GIF的路径,gif2是第二个GIF的路径,gifout是输出GIF的名称,convert是到ImageMagick的convert可执行文件的路径(如果您的路径中有ImageMagick,则可以设置convert = "magick convert")。

对于Linux,这是为了并排添加而运行的命令:

convert file1.gif'[0]' -coalesce \\( file2.gif'[0]' -coalesce \\) \\
          +append -channel A -evaluate set 0 +channel \\
          file1.gif -coalesce -delete 0 \\
          null: \\( file2.gif -coalesce \\) \\
          -gravity East -layers Composite output.gif

要垂直附加:

convert file1.gif'[0]' -coalesce \\( file2.gif'[0]' -coalesce \\) \\
          -append -channel A -evaluate set 0 +channel \\
          file1.gif -coalesce -delete 0 \\
          null: \\( file2.gif -coalesce \\) \\
          -gravity South -layers Composite output.gif

对于Mac,我不知道(我会尝试Linux方式)。

编辑

以下内容适用于任何操作系统。

appendGIFs <- function(gif1, gif2, gifout="result.gif", vertically=FALSE, 
                       delay=20, convert = "C:/PortableApps/ImageMagick/convert"){
  currentdir <- getwd()
  on.exit(setwd(currentdir))
  tmpdir <- tempdir()
  invisible(file.remove(list.files(tmpdir, full.names = TRUE, pattern = "*.gif$")))
  file.copy(gif1, to = file.path(tmpdir, "gif1.gif"))
  file.copy(gif2, to = file.path(tmpdir, "gif2.gif"))
  setwd(tmpdir)
  command <- sprintf("%s gif1.gif -coalesce gif1-%%05d.gif", convert)
  system(command)
  command <- sprintf("%s gif2.gif -coalesce gif2-%%05d.gif", convert)
  system(command)
  nframes <- length(list.files(tmpdir, pattern = "^gif1-.*gif$"))
  for(i in 1:nframes){
    command <- sprintf("%s gif1-%05d.gif gif2-%05d.gif %sappend gif-%05d.gif", 
                       convert, i-1, i-1, ifelse(vertically, "-", "+"), i)
    system(command)
  }
  command <- sprintf("%s -loop 0 -delay %d gif-*.gif result.gif", convert, delay)
  system(command)
  file.copy("result.gif", file.path(currentdir, gifout), overwrite=TRUE)
}