【问题标题】:animating only one line and two static lines in a plot with gganimate使用 gganimate 在绘图中仅对一条线和两条静态线进行动画处理
【发布时间】: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)

完成的情节应该像这张图

对我来说,这里提出了一些问题:

  1. 如何实现只有我的黑线(y 矢量)是动画的,而两条黑线(向上和向下)保持静止
  2. 以适当的方式为线条分配颜色
  3. 我的 gif 保存在哪里
  4. 是否需要从时间线 x 的开头定义我的所有行?或者是否可以在 x[10]=0.9 秒处启动 UP 向量。

【问题讨论】:

    标签: r ggplot2 gganimate


    【解决方案1】:

    问题 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())

    【讨论】:

    • 非常感谢您的详细解答!对于我的第三个问题,我的意思是当我没有指定时 gif 的存储位置。它只存储在内存中吗?
    • @tueftla 是的,它被绘制到当前的图形设备。最后一个绘图对象的副本存储在内存中,但可以通过gganimate:::.store$animation 访问并从那里保存
    • 我还有一个小问题:如何重命名图例?我找到了aes(..., fill=variable) + labs(fill="legendname") 但是当我使用它时,它不起作用。提前非常感谢!
    • @tueftla 试试color = 而不是fill =
    • 当你的意思是:aes(x = x, y = value, colour = variable) ... + labs(color = "legendname") 它也不起作用
    猜你喜欢
    • 2021-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    相关资源
    最近更新 更多