【发布时间】:2020-09-03 12:34:02
【问题描述】:
我想将一些数据动画化为一条由两条静态红线包围的黑线。静态线是 UP 和 DOWN。我的动画数据是 y,时间轴是 x。
我尝试了很多 - 使用 transition_components() 和 transition_layers() - 但没有任何效果......但也许我的代码中有一些失败我不确定......如何为我找到的行分配颜色 @987654321 @但我的处理方式不正确...
我必须完成的 gganimate 代码:
# load the needed packages
library(gifski)
library(ggplot2)
library(gganimate)
theme_set(theme_bw())
# add the time vector x
### 10.1 seconds with timestep 0.1 seconds
x=rep(seq(from=0,to=10,by=0.1),times=3)
# y should be the line in the plot which should be animated
y=c(seq(from=0,to=10,length.out=which(round(x,1)==2)[1]),rep(10,times=which(round(x,1)==5)[1]-which(round(x,1)==2)[1]),10*0.95^(seq(from=1,by=1,length.out=50)))
# UP and DOWN should be static lines in the animated plot
### there shouldn't be a "linemoving" from left to rigth side of the plot
UP=c(seq(from=1,to=12,length.out=which(round(x,1)==1.8)[1]),
rep(12,times=which(round(x,1)==5.5)[1]-which(round(x,1)==1.8)[1]),
seq(from=12,to=4,length.out=length(seq(from=0,to=10,by=0.1))-which(round(x,1)==1.8)[1]-(which(round(x,1)==5.5)[1]-which(round(x,1)==1.8)[1]))
)
DOWN=c(seq(from=0,to=8,length.out=which(round(x,1)==2.2)[1]),
rep(8,times=which(round(x,1)==4.5)[1]-which(round(x,1)==2.2)[1]),
seq(from=8,to=1,length.out=length(seq(from=0,to=10,by=0.1))-which(round(x,1)==2.2)[1]-(which(round(x,1)==4.5)[1]-which(round(x,1)==2.2)[1]))
)
value=c(y,UP,DOWN)
h=length(seq(from=0,to=10,by=0.1))
variable=c(rep("y", h), rep("UP", h), rep("DOWN", h) )
# the dataframe with the three columns
df=data.frame(x, variable, value)
p=ggplot(df, aes(x=x, y=value, group=variable, colour=variable ) ) + geom_line(size=2) + scale_color_manual(values=c("black", "red", "red")) # I want that y is a black line and UP and DOWN are red lines
# x is my time variable in the dataframe
p2=p + transition_reveal(x)
# animating p2
animate(p2)
对我来说,这里提出了一些问题:
- 如何实现只有我的黑线(y 矢量)是动画的,而两条黑线(向上和向下)保持静止
- 以适当的方式为线条分配颜色
- 我的 gif 保存在哪里
- 是否需要从时间线 x 的开头定义我的所有行?或者是否可以在 x[10]=0.9 秒处启动 UP 向量。
【问题讨论】: