如何使用 gganimate 动画让 x 轴跨度移动?

use*_*588 6 r ggplot2 gganimate

使用 R,我尝试使用 gganimate 制作一个基于 x 轴从左到右显示的折线图。我已经设法做到了这一点,但我还想做的是使scale_x_continuous(limits = c(i-5,i+5)),即在正在显示的点和窗口周围有一个窗口将继续前进,同时揭示下一点。

我尝试了很多方法来实现这一点,包括在带有和不带有 aes() 的scale_x_continuous 中实现某种循环。似乎什么都不起作用。我对 ggplot2 特别是 gganimate 很陌生,但我在网上找不到任何帮助。我有一种感觉,答案可能很简单,但我只是错过了。

有点像这样,但使用 gganimate:

类似的例子,但不是 gganimate

以下是一些可重现的代码,向您大致展示我到目前为止所做的事情。

library(ggplot2)
library(gganimate)
library(gifski)
library(png)


Step  <- c(1:50,1:50)
Name  <- c(rep("A",50), rep("B",50))
Value <- c(runif(50,0,10), runif(50,10,20))
Final <- data.frame(Step, Name, Value)

a <- ggplot(Final, aes(x = Step, y = Value, group = Name, color = factor(Name))) + 
 geom_line(size=1) + 
 geom_point(size = 2) + 
 transition_reveal(Step) + 
 coord_cartesian(clip = 'off') + 
 theme_minimal() +
 theme(plot.margin = margin(5.5, 40, 5.5, 5.5)) +
 theme(legend.position = "none") 

options(gganimate.dev_args = list(width = 7, height = 6, units = 'in', res=100))
animate(a, nframes = 100)
Run Code Online (Sandbox Code Playgroud)

Axe*_*man 5

不要使用 a transition,使用 a view。例如:

ggplot(Final, aes(x = Step, y = Value, color = factor(Name))) + 
    geom_line(size = 1) + 
    geom_point() +
    view_zoom_manual(
        0, 1, pause_first = FALSE, ease = 'linear', wrap = FALSE,
        xmin = 1:40, xmax = 11:50, ymin = min(Final$Value), ymax = max(Final$Value)
    ) +
    scale_x_continuous(breaks = seq(0, 50, 2))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述