【问题标题】:gganimate creating duplicate legend and captiongganimate 创建重复的图例和标题
【发布时间】:2017-08-14 18:01:34
【问题描述】:

我使用gganimate 创建了一个动画情节.gif。问题是输出有重复的图例和标题,我不知道是什么原因造成的。

图例应位于底部,标题应位于图的左下角。关于我在这里做错了什么有什么想法吗?

可重现的例子:

library(gapminder)
library(ggplot2)
library(gganimate)
library(viridis)

t <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent, frame = year)) +
        geom_point() +
        scale_color_viridis(name="Continent", discrete=TRUE) +
        scale_x_log10() +
        theme_void() + 
        theme( legend.position = "bottom", legend.box = "vertical", legend.title.align = 0) +
        labs(title = "Year: ") +
        labs(caption = " Caption test") +
        theme(plot.title = element_text(hjust = 0.5, vjust = 0.05)) +
        theme(plot.caption = element_text(hjust = 0, color="gray40", size=10)) 



gganimate(t, "output_test.gif")

更新 [24-03-2017]:gganimate 的作者 David Robinson 在 Twitter 上向我证实,这种奇怪的行为是由应该很快修复的错误引起的。

与此同时,@hrbrmstr 的解决方案看起来不错。另一种选择是使用旧版本的gganimate,可以这样安装:

  library(devtools)
  install_github("dgrtwo/gganimate", ref = "26ec501")

【问题讨论】:

  • 在您的可重现示例中还需要library(viridis)
  • 这是 gif 导出过程的一个怪癖:它适用于 mp4

标签: r ggplot2 gif animated-gif gganimate


【解决方案1】:

这是在gganimate 之外执行此操作的一种方法。

清理文件是留给读者的练习:-)

library(gapminder)
library(viridis)
library(magick)
library(tidyverse)

td <- tempdir()

years <- sort(unique(gapminder$year)) 

pb <- progress_estimated(length(years))

map_chr(years, ~{

  pb$tick()$print()

  filter(gapminder, year == .x) %>% 
    ggplot(aes(gdpPercap, lifeExp, size = pop, color = continent)) +
    geom_point() +
    scale_color_viridis(name="Continent", discrete=TRUE) +
    scale_x_log10() +
    labs(title = sprintf("Year: %s", .x)) +
    labs(caption = " Caption test") +
    guides(colour = guide_legend(order = 2), shape = guide_legend(order = 1)) +
    theme_void() + 
    theme(legend.position = "bottom", legend.box = "vertical", legend.title.align = 0) +
    theme(plot.title = element_text(hjust = 0.5, vjust = 0.05)) +
    theme(plot.caption = element_text(hjust = 0, color="gray40", size=10)) -> gg 

  fil <- file.path(td, sprintf("%04d.png", as.integer(.x)))

  ggsave(fil, width=5, height=3, gg)

  fil

}) %>% 
  map(image_read) %>% 
  image_join() %>% 
  image_animate(fps=2, loop=1) %>% 
  image_write("animated.gif")

【讨论】:

  • 不错的选择。谢谢!
  • 这个输出的一个小问题是所有图像的图例并不相同 - gganimate 确实确保图例保持一致 - 尽管有一些工作可能可以通过手动比例解决。
  • 是的。但你不应该依赖 gganimate 来处理那个 IMO。你应该指定你需要什么。这是个人喜好。
【解决方案2】:

gganimate_save 存在一些问题。在文档中,在详细信息下,它指出:

如果保存为 GIF,请使用利用冗余背景(比例、静态图层等)的自定义方法。

除了显示两组坐标轴的 gif 图像外,第一张图像除了水平坐标轴外都是空白的。

如果你打电话

gganimate(t, "output_test.mp4")

那么生成的电影就是预期的那样。

然后您可以在 mp4 上调用 imagemagick 以转换为 gif,在 bash 中(或适应来自 R 的系统调用):

> convert output_test.mp4 output_test.gif

来自 R:

system('convert output_test.mp4 output_test.gif')

【讨论】:

  • 谢谢。这是一个快速解决方案,我已投票支持您的回答,但它并不能解决保存 .gif 文件的问题
  • 它确实会创建一个 gif,它只是先创建一个临时 mp4 文件。无论如何,ImageMagick 是 gganimate 的先决条件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-31
  • 2019-03-29
  • 2019-03-01
  • 2021-11-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-18
相关资源
最近更新 更多