繁体   English   中英

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

[英]How to have x-axis span move with gganimate animation?

使用 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)

不要使用transition ,使用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))

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM