【问题标题】:Can I add a gif on top of another gif in R?我可以在 R 中的另一个 gif 之上添加一个 gif 吗?
【发布时间】:2020-05-10 12:41:35
【问题描述】:

我有两个用ggplotgganimmate 生成的GIF。一个是世界地图,另一个是普通条形图。我试图在世界地图内的视口中显示条形图,并让它们以相同的速率进行动画处理。由于它们都是使用相同的数据集创建的,我想我可以使用gganimate 同时为它们设置动画。

我正在寻找这样的东西:

条形图使用transition_reveal(),地图使用transition_manual(),所以我无法将它们一起制作动画。

有没有更简单的方法可以同时为两个情节制作动画?或者我可以让两者都使用相同的transition_*() 函数吗?

地图代码:

plot_anim = ggplot(data = dat) +
  geom_sf(aes(geometry = geometry,
              fill = Deaths),
          alpha = 0.3) +
  geom_point(aes(x = X, y = Y,
                 size = ifelse(Confirmed==0, NA, Confirmed)),
             shape = 21,
             colour = "turquoise",
             alpha = 0.5,
             fill = "turquoise") +
  transition_manual(Date)

animate(p, renderer = gifski_renderer(loop = T))

用于条形图的代码:

plot_anim2 = ggplot(data = totals) +
  geom_bar(aes(x = Date, y = Number,
               fill = Type),
           stat = "identity",
           position = "stack") +
  transition_time(Date) +
  shadow_mark(past = T)  

animate(plot_anim2, renderer = gifski_renderer(loop = TRUE))

编辑:

一些数据:

> head(dat)
        Date             Country Confirmed Recovered Deaths      pop cases_per_mil          X         Y
1 2020-01-22         Afghanistan         0         0      0 29117000             0  66.004734  33.83523
2 2020-01-22             Albania         0         0      0  3195000             0  20.049834  41.14245
3 2020-01-22             Algeria         0         0      0 35423000             0   2.617323  28.15894
4 2020-01-22             Andorra         0         0      0    84082             0   1.560544  42.54229
5 2020-01-22              Angola         0         0      0 18993000             0  17.537368 -12.29336
6 2020-01-22 Antigua and Barbuda         0         0      0    89000             0 -61.794693  17.27750
                        geometry
1 MULTIPOLYGON (((74.89131 37...
2 MULTIPOLYGON (((20.06396 42...
3 MULTIPOLYGON (((8.576563 36...
4 MULTIPOLYGON (((1.706055 42...
5 MULTIPOLYGON (((14.19082 -5...
6 MULTIPOLYGON (((-61.71606 1...
> head(totals)
# A tibble: 6 x 3
  Date       Type      Number
  <date>     <chr>      <int>
1 2020-01-22 Confirmed    555
2 2020-01-23 Confirmed    654
3 2020-01-24 Confirmed    941
4 2020-01-25 Confirmed   1434
5 2020-01-26 Confirmed   2118
6 2020-01-27 Confirmed   2927

【问题讨论】:

  • 听起来是个有趣的问题。您有机会与我们分享一些数据吗?即“dat”和“totals”。
  • 抱歉,已经添加了一些!
  • 您可以试试这个:stackoverflow.com/q/49511083/2554330,在命令行上使用 ImageMagick,或者尝试 R 中的 magick 包在 R 中完成所有操作。
  • 不幸的是,这个数据 sn-p 不允许我们重现数据(查看所有省略号...)您能否发布 dput(head(dat))dput(head(totals)) 的完整输出
  • @Tjebo 我在控制台中运行了它,但由于几何数据,输出非常长。我会说here for dathere for totals

标签: r ggplot2 spatial gganimate


【解决方案1】:

实现此目的的一种方法是将您的 gif 文件保存到 www 文件夹并使用 absolutePanel 在空白的 Shiny 应用程序中显示它们。您可以让条形图褪色,而另一个不褪色。下面是一个静态世界地图,其中两个图像显示在绝对面板中。当它们浮动时,您可以将它们拖放到您想要的位置。

【讨论】:

    猜你喜欢
    • 2016-02-28
    • 2011-03-09
    • 2018-09-05
    • 2013-09-24
    • 2019-02-04
    • 2019-06-26
    • 2016-09-13
    • 2021-12-02
    • 2014-01-02
    相关资源
    最近更新 更多