首页 > 解决方案 > 将两个动画情节组合成一个 GIF/MP4

问题描述

我创建了两个基于相同数据集的动画时间序列图表。一个是显示总体趋势的折线图,一个是显示前 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"))

正在研究如何将这两个动画并排或以折线图作为插图组合成一个动画。

标签: rggplot2gganimate

解决方案


你可以用 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是第一个 GIFgif2的路径,是第二个 GIF 的路径, gifout是输出 GIF 的名称,convert是 ImageMagick 的可执行文件的路径(如果 ImageMagick 在你的路径中,convert你可以设置)。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)
}

推荐阅读