如何使用 gganimate 动画让 x 轴跨度移动?
How to have x-axis span move with gganimate animation?
使用 R,我正在尝试使用 gganimate 制作一个基于 x 轴从左到右显示的折线图。我设法做到了这一点,但我还想做的是使 scale_x_continuous(limits = c(i-5,i+5)),即周围有一个 window显示的点,window 将在显示下一个点的同时移动。
我尝试了很多方法来实现这一点,包括在 scale_x_continuous 中使用和不使用 aes() 实现某种循环。似乎没有任何效果。我对 ggplot2 很陌生,尤其是 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))
使用 R,我正在尝试使用 gganimate 制作一个基于 x 轴从左到右显示的折线图。我设法做到了这一点,但我还想做的是使 scale_x_continuous(limits = c(i-5,i+5)),即周围有一个 window显示的点,window 将在显示下一个点的同时移动。
我尝试了很多方法来实现这一点,包括在 scale_x_continuous 中使用和不使用 aes() 实现某种循环。似乎没有任何效果。我对 ggplot2 很陌生,尤其是 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))