顺序控制图
Sequential gganimate plot
我正在尝试使用 gganimate
创建动画时间序列图。以下代码几乎可以产生所需的输出。它创建两个随机时间序列,构造一个数据帧并渲染所需的动画:
# necessary packages
require(ggplot2)
require(gganimate)
require(transformr)
require(gifski)
# construct data frame with two random time series and time index t
TS = matrix(rnorm(100 * 2), 100, 2)
plot.df = data.frame(TS, t = 1:100)
plot.df = reshape2::melt(plot.df, id.vars = "t")
# usual ggplot line plot structure
p = ggplot(plot.df, aes(x = t, y = value, col = variable)) +
geom_line()
# animation that reveals time series along time dimension (= x-axis)
play = p + transition_reveal(t)
# render plot using gifski
animate(play,renderer = gifski_renderer())
In the output, both time series are revealed at the same time.是否可以选择先显示第一个时间序列,然后在第一个时间序列之上显示第二个时间序列?
有什么可以提供帮助的解决方法。这个想法是创建一个从数据集的 1 到 nrow
递增的新列,并在 transition_reveal()
中使用它:
显然,数据集应该像您的数据一样按时间和变量排序。
# necessary packages
require(ggplot2)
require(gganimate)
require(gifski)
# construct data frame with two random time series and time index t
TS <- matrix(rnorm(100 * 2), 100, 2)
plot.df <- data.frame(TS, t = 1:100)
plot.df <- reshape2::melt(plot.df, id.vars = "t")
# reorder in case
# plot.df <- plot.df[order(plot.df$variable,plot.df$t),]
# adding a new column to make progressive the reveal of the data
plot.df$transition_value <- 1:nrow(plot.df)
# your ggplot plot
p <- ggplot(plot.df, aes(x = t, y = value, col = variable)) +
geom_line()
# reveal with the new value
play <- p + transition_reveal(transition_value)
# render plot using gifski
animate(play,renderer = gifski_renderer())
我正在尝试使用 gganimate
创建动画时间序列图。以下代码几乎可以产生所需的输出。它创建两个随机时间序列,构造一个数据帧并渲染所需的动画:
# necessary packages
require(ggplot2)
require(gganimate)
require(transformr)
require(gifski)
# construct data frame with two random time series and time index t
TS = matrix(rnorm(100 * 2), 100, 2)
plot.df = data.frame(TS, t = 1:100)
plot.df = reshape2::melt(plot.df, id.vars = "t")
# usual ggplot line plot structure
p = ggplot(plot.df, aes(x = t, y = value, col = variable)) +
geom_line()
# animation that reveals time series along time dimension (= x-axis)
play = p + transition_reveal(t)
# render plot using gifski
animate(play,renderer = gifski_renderer())
In the output, both time series are revealed at the same time.是否可以选择先显示第一个时间序列,然后在第一个时间序列之上显示第二个时间序列?
有什么可以提供帮助的解决方法。这个想法是创建一个从数据集的 1 到 nrow
递增的新列,并在 transition_reveal()
中使用它:
显然,数据集应该像您的数据一样按时间和变量排序。
# necessary packages
require(ggplot2)
require(gganimate)
require(gifski)
# construct data frame with two random time series and time index t
TS <- matrix(rnorm(100 * 2), 100, 2)
plot.df <- data.frame(TS, t = 1:100)
plot.df <- reshape2::melt(plot.df, id.vars = "t")
# reorder in case
# plot.df <- plot.df[order(plot.df$variable,plot.df$t),]
# adding a new column to make progressive the reveal of the data
plot.df$transition_value <- 1:nrow(plot.df)
# your ggplot plot
p <- ggplot(plot.df, aes(x = t, y = value, col = variable)) +
geom_line()
# reveal with the new value
play <- p + transition_reveal(transition_value)
# render plot using gifski
animate(play,renderer = gifski_renderer())