使用gtable对象与gganimate

3
我正在使用gganimate制作gif,我想通过将ggplot对象转换为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)

enter image description here

如何在gganimate中使用此函数?

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

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

2
这是结果。下面有解释:

animated plot

任何与grobs相关的黑客攻击都应在动画绘图的各个帧被创建之后,但在它们被绘制在相关的图形设备上之前进行。这个窗口出现在gganimate:::Sceneplot_frame函数中。
我们可以定义一个继承自原始版本的自己的Scene版本,但使用插入了grob hack行的修改过的plot_frame函数:
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)
  })

此后,我们必须在动画过程中用我们的版本 Scene2 替换 Scene。以下是两种方法:
  1. 定义一个单独的动画函数 animate2,以及必要的中间函数,以使用 Scene2 替代 Scene。在我看来,这是更安全的做法,因为它不会改变 gganimate 包中的任何内容。但是,它需要更多的代码,并且如果源代码中的函数定义发生更改,它可能在将来出现问题。

  2. 覆盖 gganimate 包中的现有函数,仅限于本次会话(基于 这里 的答案)。这需要每次手动努力,但实际上所需的代码更少,而且可能不容易出错。但是,它也存在混淆用户的风险,因为同一个函数可能会导致不同的结果,具体取决于它是在更改之前还是之后调用的。

方法 1

定义函数:

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))

方法2

在控制台中运行trace(gganimate:::create_scene, edit=TRUE),并在弹出的编辑窗口中将Scene更改为Scene2

用法:

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

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

1
这太棒了!非常感谢您对这两种方法的优缺点发表的评论。看起来方法1是最安全的,可以通过将该代码存储在辅助脚本中(例如“Custom_Animation_Functions.R”),并从主脚本(例如“Produce_Animated_Plots.R”)中进行调用来实现。 - bschneidr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接