将 gtable object 与 gganimate 一起使用

Using a gtable object with gganimate

我正在用 gganimate 制作一个 gif,我想对绘图格式做一些调整,这只能通过将 ggplot object 转换为一个gtable。例如,我想更改绘图标题的位置,使其始终出现在绘图的最左角。

下面是情节调整的示例:

library(ggplot2)
library(gganimate)
library(dplyr)

# Helper function to position plot title all the way to left of plot
align_titles_left <- function(p, newpage = TRUE) {
  p_built <- invisible(ggplot2::ggplot_build(p))
  gt <- invisible(ggplot2::ggplot_gtable(p_built))
  
  gt$layout[which(gt$layout$name == "title"), c("l", "r")] <- c(2, max(gt$layout$r))
  gt$layout[which(gt$layout$name == "subtitle"), c("l", "r")] <- c(2, max(gt$layout$r))
  
  
  # Prints the plot to the current graphical device
  # and invisibly return the object
  gridExtra::grid.arrange(gt, newpage = newpage)
  invisible(gt)
}

# Create an example plot
static_plot <- iris %>% 
  ggplot(aes(x = Sepal.Length, y = Sepal.Width,
             color = Species)) +
  geom_point() +
  labs(title = "This title should appear in the far left.")

# Print the static plot using the adjustment function
align_titles_left(static_plot)

如何将此功能与 gganimate 一起使用?

下面是一些示例 gganimate 代码,可将此示例中的情节转换为动画。

# Produce the animated plot
static_plot +
  transition_states(Species,
                    transition_length = 3,
                    state_length = 1)

这是结果。解释如下:

任何使用 grobs 的黑客攻击都应该在 动画情节的各个帧创建之后,但在它们被绘制到相关的图形设备。这个window出现在gganimate:::Sceneplot_frame函数中。

我们可以定义我们自己的 Scene 版本,它继承了原始版本,但使用了修改后的 plot_frame 函数,其中插入了 grob hack 行:

Scene2 <- ggproto(
  "Scene2",
  gganimate:::Scene,
  plot_frame = function(self, plot, i, newpage = is.null(vp), 
                        vp = NULL, widths = NULL, heights = NULL, ...) {
    plot <- self$get_frame(plot, i)
    plot <- ggplot_gtable(plot)

    # insert changes here
    plot$layout[which(plot$layout$name == "title"), c("l", "r")] <- c(2, max(plot$layout$r))
    plot$layout[which(plot$layout$name == "subtitle"), c("l", "r")] <- c(2, max(plot$layout$r))

    if (!is.null(widths)) plot$widths <- widths
    if (!is.null(heights)) plot$heights <- heights
    if (newpage) grid::grid.newpage()
    grDevices::recordGraphics(
      requireNamespace("gganimate", quietly = TRUE),
      list(),
      getNamespace("gganimate")
    )
    if (is.null(vp)) {
      grid::grid.draw(plot)
    } else {
      if (is.character(vp)) seekViewport(vp)
      else pushViewport(vp)
      grid::grid.draw(plot)
      upViewport()
    }
    invisible(NULL)
  })

此后,我们必须在动画过程中将Scene替换为我们的版本Scene2。我在下面列出了两种方法:

  1. 定义一个单独的动画函数,animate2,加上使用 Scene2 而不是 Scene 所需的中间函数。在我看来,这更安全,因为它不会更改 gganimate 包中的任何内容。但是,它确实涉及更多代码,并且如果函数定义在源代码处发生更改,将来可能会中断。

  2. 覆盖 gganimate 程序包中的现有函数 用于此会话 (基于答案 here)。这需要在每个会话中进行手动操作,但所需的实际代码更改非常小,而且可能不会那么容易中断。但是,它也存在混淆用户的风险,因为相同的函数可能导致不同的结果,具体取决于它是在更改之前还是之后调用。

方法一

定义函数:

library(magrittr)

create_scene2 <- function(transition, view, shadow, ease, transmuters, nframes) {
  if (is.null(nframes)) nframes <- 100
  ggproto(NULL, Scene2, transition = transition, 
          view = view, shadow = shadow, ease = ease, 
          transmuters = transmuters, nframes = nframes)
}

ggplot_build2 <- gganimate:::ggplot_build.gganim
body(ggplot_build2) <- body(ggplot_build2) %>%
  as.list() %>%
  inset2(4,
         quote(scene <- create_scene2(plot$transition, plot$view, plot$shadow, 
                                      plot$ease, plot$transmuters, plot$nframes))) %>%
  as.call()

prerender2 <- gganimate:::prerender
body(prerender2) <- body(prerender2) %>%
  as.list() %>%
  inset2(3,
         quote(ggplot_build2(plot))) %>%
  as.call()

animate2 <- gganimate:::animate.gganim
body(animate2) <- body(animate2) %>%
  as.list() %>%
  inset2(7,
         quote(plot <- prerender2(plot, nframes_total))) %>%
  as.call()

用法:

animate2(static_plot +
           transition_states(Species,
                             transition_length = 3,
                             state_length = 1))

方法二

运行 trace(gganimate:::create_scene, edit=TRUE) 在控制台中,& 在弹出编辑 window.

中将 Scene 更改为 Scene2

用法:

animate(static_plot +
          transition_states(Species,
                            transition_length = 3,
                            state_length = 1))

(两种方法的结果相同。)