首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用gganimate使用gtable对象

用gganimate使用gtable对象
EN

Stack Overflow用户
提问于 2019-03-26 17:24:58
回答 1查看 410关注 0票数 3

我正在用gganimate生成一个gif,我想对绘图格式做一些调整,这些调整只能通过将ggplot对象转换为gtable来完成。例如,我想更改情节标题的位置,以便它总是出现在情节的左下角。

下面是情节调整的一个例子:

代码语言:javascript
复制
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代码,可以将此示例中的情节转换为动画。

代码语言:javascript
复制
# Produce the animated plot
static_plot +
  transition_states(Species,
                    transition_length = 3,
                    state_length = 1)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-27 04:58:34

结果是这样的。解释如下:

任何与grobs的黑客行为都应该在之后进行,在之后,动画情节的各个帧已经创建,但在相关的图形设备上绘制之前。此窗口发生在gganimate:::Scenegganimate:::Scene函数中。

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

代码语言:javascript
复制
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)。这要求每个会话都需要手工操作,但是所需的实际代码更改非常小&可能不会那么容易中断。但是,它也有混淆用户的风险,因为相同的函数可能导致不同的结果,这取决于更改之前还是之后调用它。

方法1

界定职能:

代码语言:javascript
复制
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()

用法:

代码语言:javascript
复制
animate2(static_plot +
           transition_states(Species,
                             transition_length = 3,
                             state_length = 1))

方法2

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

用法:

代码语言:javascript
复制
animate(static_plot +
          transition_states(Species,
                            transition_length = 3,
                            state_length = 1))

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55362961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档