r - 在 gganimate 中使用 transition_reveal 来累积绘制线条

标签 r gganimate

有没有办法在 gganimate 中使用 transition_reveal() 来一条一条地画线 - 或者使用其他过渡函数的解决方法..

library(tidyverse)
library(gganimate)
set.seed(1)
d <- tibble(x = 1:100, a = cumsum(rnorm(100)), b = cumsum(rnorm(100))) %>%
  pivot_longer(cols = a:b, names_to = "grp", values_to = "y")
d
# # A tibble: 200 x 3
#        x grp        y
#    <int> <chr>  <dbl>
#  1     1 a     -0.626
#  2     1 b     -0.620
#  3     2 a     -0.443
#  4     2 b     -0.578
#  5     3 a     -1.28 
#  6     3 b     -1.49 
#  7     4 a      0.317
#  8     4 b     -1.33 
#  9     5 a      0.646
# 10     5 b     -1.99 
# # ... with 190 more rows

我希望一次只显示一条线 - 以便在第一条线完成后开始绘制第二条线(在第 101 帧上) - 而不是同时绘制两条线...

ggplot(data = d, mapping = aes(x = x, y = y, colour = grp)) +
  geom_line() +
  transition_reveal(along = x) 

enter image description here

最佳答案

我们可以创建一个辅助列,将 b 的点放在 a 的所有点之后:

d %>%
  arrange(grp, x) %>%
  mutate(x_reveal = row_number()) %>%
  ggplot(aes(x = x, y = y, colour = grp)) +
    geom_line() +
    transition_reveal(along = x_reveal) 

enter image description here

关于r - 在 gganimate 中使用 transition_reveal 来累积绘制线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58435664/

相关文章:

R 编程 : sample() function return repeated entities even replace=TRUE

r - .jcall(cell, "V", "setCellValue", value) : method setCellValue with signature ([D)V not found when attempting write. xlsx 中的错误

r - Shiny 的多重动态子集

r - R6 类的 S4 调度行为不一致

r - 如何使用 rmarkdown::render() 在 html 中呈现 gganimate 图,而不产生不需要的输出

r - gganimate 图中的标签行为问题

r - 在 R 中使用正则表达式提取文本时出错

r - 使用 transition_time 随时间 gganimate,其中时间变量是 NBA 比赛时钟

r - 将预制的 gganimate gif 加载到 rmarkdown block 中作为标题