簡體   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