使用 gganimate 在绘图中仅设置一条线和两条静态线的动画
animating only one line and two static lines in a plot with gganimate
我想将一些数据设置为由两条静态红线包围的黑线的动画。静态线为 UP 和 DOWN。我的动画数据在y,时间线在x.
我尝试了很多 - 使用 transition_components()
和 transition_layers()
- 但没有任何效果......但也许我的代码有一些失败我不确定......如何分配颜色我找到的行 Changing line colors with ggplot() 但我的处理不当...
我要完成的 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 向量。
问题 1 是最棘手的。您需要组织数据,使两个静态行的 x 值具有不同的名称(例如 static_x
),并且没有任何实际的 x
值。将 df 分成两个不同的帧可能是最简单的方法:
df_y <- df[variable == "y", ]
df_not_y <- df[variable != "y",]
df_not_y$static_x <- df_not_y$x
df_not_y <- df_not_y[names(df_not_y) != "x"]
然后,为了处理问题4,我们将在1秒前删除变量UP
的所有值:
df_not_y$value[df_not_y$static_x < 1 & df_not_y$variable == "UP"] <- NA
现在我们可以绘图了。我们需要 两次 geom_line
次调用,一次用于静态变量,一次用于移动变量。为了回答问题 2,我们在 scale_color_manual
中按名称分配颜色,以确保级别正确。
p <- ggplot(df_y, aes(x = x, y = value, colour = variable)) +
geom_line(aes(x = static_x), data = df_not_y, size = 2) +
geom_line(size = 2) +
scale_color_manual(values = c(UP = "red", y = "black", DOWN = "red"))
p2 <- p + transition_reveal(x)
animate(p2)
剩下问题 3。在这种情况下,我只需右键单击查看器面板并选择“保存图像”,但如果您觉得这样更方便,您也可以gganimate::anim_save("mygif.gif", last_animation())
。
我想将一些数据设置为由两条静态红线包围的黑线的动画。静态线为 UP 和 DOWN。我的动画数据在y,时间线在x.
我尝试了很多 - 使用 transition_components()
和 transition_layers()
- 但没有任何效果......但也许我的代码有一些失败我不确定......如何分配颜色我找到的行 Changing line colors with ggplot() 但我的处理不当...
我要完成的 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 向量。
问题 1 是最棘手的。您需要组织数据,使两个静态行的 x 值具有不同的名称(例如 static_x
),并且没有任何实际的 x
值。将 df 分成两个不同的帧可能是最简单的方法:
df_y <- df[variable == "y", ]
df_not_y <- df[variable != "y",]
df_not_y$static_x <- df_not_y$x
df_not_y <- df_not_y[names(df_not_y) != "x"]
然后,为了处理问题4,我们将在1秒前删除变量UP
的所有值:
df_not_y$value[df_not_y$static_x < 1 & df_not_y$variable == "UP"] <- NA
现在我们可以绘图了。我们需要 两次 geom_line
次调用,一次用于静态变量,一次用于移动变量。为了回答问题 2,我们在 scale_color_manual
中按名称分配颜色,以确保级别正确。
p <- ggplot(df_y, aes(x = x, y = value, colour = variable)) +
geom_line(aes(x = static_x), data = df_not_y, size = 2) +
geom_line(size = 2) +
scale_color_manual(values = c(UP = "red", y = "black", DOWN = "red"))
p2 <- p + transition_reveal(x)
animate(p2)
剩下问题 3。在这种情况下,我只需右键单击查看器面板并选择“保存图像”,但如果您觉得这样更方便,您也可以gganimate::anim_save("mygif.gif", last_animation())
。