标签: gganimate

多个 gganimate 图堆叠和并排

我有四个具有相同帧数的动画图,其中我想要三个堆叠的动画和三个堆叠动画右侧的第四个。通常我会使用类似 grid.arrange 的东西,但这似乎不适用于 gganimate。

我根据这里的代码得到了以下代码:https : //github.com/thomasp85/gganimate/wiki/Animation-Composition

a_mgif <- image_read(a_gif)
b_mgif <- image_read(b_gif)
c_mgif <- image_read(c_gif)
d_mgif <- image_read(d_gif)

new_gif <- image_append(c(a_mgif[1], b_mgif[1], c_mgif[1], d_mgif[1]), stack = TRUE)

for(i in 2:100){
  combined <- image_append(c(a_mgif[i], b_mgif[i], c_mgif[i], d_mgif[i]), stack = TRUE)
  new_gif <- c(new_gif, combined)
}

magick::image_write(new_gif, path="animation.1.gif")
Run Code Online (Sandbox Code Playgroud)

这工作正常并产生四个堆叠的动画图。我尝试将其修改为在前三个堆叠图右侧的第四个图,如下所示:

new_gif.1 <- image_append(c(a_mgif[1], b_mgif[1], c_mgif[1]), stack = TRUE)
new_gif.2 <- image_append(c(new_gif.1, d_mgif[1]), stack = FALSE)

for(i in 2:100){
  combined.1 <- image_append(c(a_mgif[i], b_mgif[i], c_mgif[i]), stack = TRUE)
  combined.2 <- image_append(c(combined.1, d_mgif[i]), stack …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gganimate

10
推荐指数
1
解决办法
958
查看次数

无法转换标题或转换时间取决于在新版本的 gganimate 中使用 transition_states 或 transition_times

我一直在玩 gganimate 的新版本,我在课堂上经常使用动画。我正在尝试构建一个图表,显示西班牙的一个站点中一氧化二氮如何随时间变化。我想要动画中的两个功能

  1. 每年停下来一段时间
  2. 每次都在标题中注明年份

我已经能够使用以下数据构建这两个图

Madrid3 <- structure(list(month = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
                     1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 
                     9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 
                     4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gganimate

9
推荐指数
1
解决办法
1049
查看次数

格尼玛特图,点保持不变,线变淡

这是一个静态图的可复制示例,我想对其进行动画处理(我想显示MCMC采样器的行为)。

library(tidyverse)
library(gganimate)

set.seed(1234)
plot_data <- tibble(x=cumsum(rnorm(100)),
                    y=cumsum(rnorm(100)),
                    time=1:length(x)) 

ggplot(data=plot_data,
       aes(x=y, y=x)) +
  geom_point() + geom_line()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我想看到的是绘制点时它们是可见的,然后一点点褪色(即alpha从例如1变为0.3),而会有一条线仅显示最近的历史(理想情况下是淡色显示最近的历史最少消失,多退后几步完全消失)。

以下内容或多或少地实现了我想要的点(因此,从某种意义上讲,我只想向此点添加渐变线以连接最后几个点-点在某些帧上越慢渐变越好):

ggplot(data=plot_data,
       aes(x=y, y=x)) +
  geom_point() +
  transition_time(time) +
  shadow_mark(past = T, future=F, alpha=0.3)
Run Code Online (Sandbox Code Playgroud)

显示点应如何褪色的图

我正在努力的是如何为两个几何(例如点和线)添加两个不同的行为。例如,在下面的点消失了(我不希望它们消失),线条也没有褪色(我希望它们消失)。

p <- ggplot(data=plot_data,
       aes(x=y, y=x)) +
  geom_point() +
  transition_time(time) +
  shadow_mark(past = T, future=F, alpha=0.3)

p + geom_line() +
  transition_reveal(along = time) +
  shadow_mark(past = T, future=F, alpha=0.3) 
Run Code Online (Sandbox Code Playgroud)

animation r ggplot2 gganimate

9
推荐指数
1
解决办法
198
查看次数

错误:动画对象未指定 save_animation 方法

我试图按照下面的方法用 R 创建一个条形图竞赛。当我尝试将它们另存为 gif 文件时,所有 png 都会被创建,然后我得到:

Error: The animation object does not specify a save_animation method
Run Code Online (Sandbox Code Playgroud)

是否因为我创建了多个文件而发生这种情况?

#This script contains a function to produce a "bar chart race" in R

#The following links were invaluable:
#https://www.blakeshaffer.ca/post/making-animated-charts-with-gganimate/
#/sf/ask/3683660571/


#load required packages:
library(tidyverse)
library(gganimate)


#inputs:
#data - the dataset, must contain a column called "year"
#x - the column which contains the numeric value to plot 
#y - the column which contains the labels of the plot

#optional: 
#title - title …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gganimate

9
推荐指数
2
解决办法
7627
查看次数

与geom_bar有关的问题?

自从David Robinson发布他的gganimate包并认为我自己有一个游戏以来,我一直羡慕和钦佩在Twitter上出现的各种ggplot动画.使用geom_bar时,我遇到了gganimate的问题.希望以下示例演示此问题.

首先为可重现的示例生成一些数据:

df <- data.frame(x = c(1, 2, 1, 2),
                 y = c(1, 2, 3, 4),
                 z = c("A", "A", "B", "B"))
Run Code Online (Sandbox Code Playgroud)

为了证明我正在尝试做什么,我认为绘制一个普通的ggplot是有用的z.我正试图让gganimate产生一个在这两个图之间循环的gif.

ggplot(df, aes(x = x, y = y)) +
   geom_bar(stat = "Identity") +
   facet_grid(~z)
Run Code Online (Sandbox Code Playgroud)

facetted_barchart

但是当我使用gganimate时,B的情节表现得很奇怪.在第二帧中,条形从第一帧条形结束的值开始,而不是从原点开始.好像它是一个堆积的条形图.

p <- ggplot(df, aes(x = x, y = y, frame = z)) +
   geom_bar(stat = "Identity") 
gg_animate(p)
Run Code Online (Sandbox Code Playgroud)

bars_animation

顺便说一下,当尝试相同的情节时,geom_point一切都按预期工作.

q <- ggplot(df, aes(x = x, y = y, frame = z)) +
    geom_point() 
gg_animate(q)
Run Code Online (Sandbox Code Playgroud)

我试图发布一些图片,但显然我没有足够的声誉,所以我希望没有它们就有意义.这是一个错误,还是我错过了什么?

提前致谢,

托马斯

r ggplot2 gganimate

8
推荐指数
1
解决办法
1606
查看次数

gganimate如何订购有序的酒吧时间序列?

我有一个时间序列的数据,我在y轴上绘制疾病的诊断率DIAG_RATE_65_PLUS,并在x轴上绘制地理组NAME作为简单的条形图.我的时间变量是ACH_DATEyearmon动画在标题中看到的循环.

df %>% ggplot(aes(reorder(NAME, DIAG_RATE_65_PLUS), DIAG_RATE_65_PLUS)) +
  geom_bar(stat = "identity", alpha = 0.66) +
  labs(title='{closest_state}') +
  theme(plot.title = element_text(hjust = 1, size = 22),
        axis.text.x=element_blank()) +
  transition_states(ACH_DATEyearmon, transition_length = 1, state_length = 1) +
  ease_aes('linear')
Run Code Online (Sandbox Code Playgroud)

我已经重新排序,NAME所以它排名靠前DIAG_RATE_65_PLUS.

gganimate产生的是什么:

有趣的情节

我现在有两个问题:

1)gganimate如何重新排序数据?有一些整体的一般重新排序,但每个月没有框架,DIAG_RATE_65_PLUS从最小到最大的组完美排序.理想情况下,我希望最后一个月"2018年8月"完美订购.所有前几个月的x轴都可以根据NAME"2018年8月" 的订购而定.

2)在gganimate中是否有一个选项,其中组在条形图中每个月"转移"到正确的等级?

我的评论查询图:

https://i.stack.imgur.com/s2UPw.gif https://i.stack.imgur.com/Z1wfd.gif

@JonSpring

    df %>%
  ggplot(aes(ordering, group = NAME)) +
  geom_tile(aes(y = DIAG_RATE_65_PLUS/2, 
                height = DIAG_RATE_65_PLUS,
                width = 0.9), alpha = 0.9, fill = …
Run Code Online (Sandbox Code Playgroud)

animation r ggplot2 dplyr gganimate

8
推荐指数
2
解决办法
2015
查看次数

gganimate 不显示所有帧

我想制作一个包含 138 个状态的简单 gif 。我不需要每个状态之间有任何花哨的转换,但我确实需要每个状态都出现在我的 gif 中。目前,gganimate正在缩短我的 gif,不透露最后大约 30 种状态。

例子

df <- lapply(1:138, function(t){
  data.frame(
    DT = as.POSIXct("2019-01-01 12:00:00", tz = "UTC") + t*24*60*60,
    x = seq(-3, 3, length.out = 100),
    y = t * seq(-3, 3, length.out = 100)
  )
})
df <- do.call(rbind, df)
range(df$DT)  # "2019-01-02 12:00:00 UTC" "2019-05-19 12:00:00 UTC"

p <- ggplot(data = df, mapping = aes(x = x, y = y)) + 
  geom_point()+ 
  transition_states(states = DT, transition_length = 0, …
Run Code Online (Sandbox Code Playgroud)

r animated-gif gganimate

8
推荐指数
2
解决办法
2342
查看次数

Speed Up gganimate Rendering

I am currently creating a gif which contains large data. I want it in high resolution.

On my old PC it took hours to render and simply wasn't worth it.

My new PC has a very strong intel i9-9900k core processor which has sped it up to about 30 minutes but R only uses 1 core by default... I have 8 available and it would be great if I could just use them to get the rendering done in under …

multithreading r ggplot2 rstudio gganimate

8
推荐指数
1
解决办法
184
查看次数

gganimate 返回 .png 文件,但没有动画对象

我正在尝试创建一个动画来向学生展示如何用动画表示数据。运行下面的代码

library(ggplot)
library(gganimate)
library(carData)        
anim <- ggplot(mtcars, aes(mpg, disp)) +
          transition_states(gear, transition_length = 2, state_length = 1) +
          enter_fade() +
          exit_fade()

    animate(anim)
Run Code Online (Sandbox Code Playgroud)

我期待预览窗口上的动画和 gif 图像的创建。

相反,我得到的是工作目录中的 100 个 .png 文件,但没有别的。图片是正确的,是动画的帧,只是没有被函数拼凑起来。特别是我得到了 100 个元素,其中我在这里显示了第一个元素:这个列表

  [1] "./gganim_plot0001.png" "./gganim_plot0002.png" "./gganim_plot0003.png" 
  [4] "./gganim_plot0004.png" "./gganim_plot0005.png" "./gganim_plot0006.png"
  [7] "./gganim_plot0007.png" "./gganim_plot0008.png" "./gganim_plot0009.png"
 [10] "./gganim_plot0010.png" "./gganim_plot0011.png" "./gganim_plot0012.png"...
Run Code Online (Sandbox Code Playgroud)

    attr(,"frame_vars")
    frame nframes progress transitioning previous_state closest_state
1       1     100     0.01         FALSE              3             3
2       2     100     0.02         FALSE              3             3
3       3     100     0.03         FALSE              3             3
Run Code Online (Sandbox Code Playgroud)

next_state
1 …
Run Code Online (Sandbox Code Playgroud)

rendering ggplot2 gganimate

8
推荐指数
1
解决办法
3169
查看次数

gganimate 情节没有显示和保存一堆 .png 的

我试图gganimate这里复制一个简单的例子。问题是简单的ggplot部分工作正常但是当我添加transition_states rstudio开始渲染然后保存一堆gganim_plotXXXX.png文件并且没有显示预期的动画时。

第一个问题显然是:我做错了什么以至于动画不起作用?

第二个问题是:保存一堆 .png 文件是标准的gganimate吗?

提前致谢

library(gganimate)

p <- ggplot(iris, aes(x = Petal.Width, y = Petal.Length)) + 
         geom_point()

anim <- p + 
  transition_states(Species,
                    transition_length = 2,
                    state_length = 1)
anim
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gganimate

8
推荐指数
1
解决办法
2331
查看次数