使用 GGanimate 使分组散点图中的组点在所有点出现后消失
Using GGanimate make points of group in a grouped scatter plot disappear once all of them appeared
我正在尝试制作一个分组散点图,其中点出现在不同的时间点(基于时间变量),并且一旦某个组的所有点都出现,它们就会全部消失。不同的组不在同一时间点开始,也不在同一终点结束,也没有相同数量的数据点。
我想这应该是 transition_time()
(在实际数据中使用实际时间戳)、exit_disappear()
and/or shadow_mark()
的某种组合,但我不能似乎让这个工作。
我试过了,但没有得到我需要的结果。例如,我需要 A 组全部一个接一个出现,然后在时间点 15 之后消失。并且需要 B 组全部在时间点 21 之后开始出现,一个接一个地出现,然后在时间点 45 之后消失。
library(ggplot2)
library(gganimate)
library(tibble)
# data
x <- c(-15:-1, -5:19, 1:45, -10:4)
y <- x + rnorm(100, 0, 2) + c(rep(-10, 15), rep(20, 25), rep(-5, 45), rep(8, 15))
id <- c(rep("A", 15), rep("B", 25), rep("C", 45), rep("D", 15))
time <- c(1:15, 21:45, 1:45, 41:55)
tib <- tibble(x,y, id, time)
# does not disappear after one id is done
anim_tib1 <- ggplot(tib, aes(x = x, y = y, col = id, group = id))+
geom_point(size = 2)+
transition_time(time = time)+
exit_disappear()+
shadow_mark()
# this works, but now there is no trail
anim_tib2 <- ggplot(tib, aes(x = x, y = y, col = id, group = id))+
geom_point(size = 2)+
transition_components(time)+
exit_disappear()
# this almost yields the same result as the first try
anim_tib3 <- ggplot(tib, aes(x = x, y = y, col = id, group = id))+
geom_point(size = 2)+
transition_components(time)+
exit_disappear()+
shadow_mark()
我认为 shadow_wake
而不是 shadow_mark
可能会产生您正在寻找的结果:
# modified code from anim_tib1
ggplot(tib, aes(x = x, y = y, col = id, group = id)) +
geom_point(size = 2) +
transition_time(time = time) +
exit_disappear() +
shadow_wake(wake_length = 1, size = NULL, alpha = NULL, wrap = FALSE)
数据:
set.seed(123)
x <- c(-15:-1, -5:19, 1:45, -10:4)
y <- x + rnorm(100, 0, 2) + c(rep(-10, 15), rep(20, 25), rep(-5, 45), rep(8, 15))
id <- c(rep("A", 15), rep("B", 25), rep("C", 45), rep("D", 15))
time <- c(1:15, 21:45, 1:45, 41:55)
tib <- tibble(x, y, id, time)
rm(x, y, id, time)
我正在尝试制作一个分组散点图,其中点出现在不同的时间点(基于时间变量),并且一旦某个组的所有点都出现,它们就会全部消失。不同的组不在同一时间点开始,也不在同一终点结束,也没有相同数量的数据点。
我想这应该是 transition_time()
(在实际数据中使用实际时间戳)、exit_disappear()
and/or shadow_mark()
的某种组合,但我不能似乎让这个工作。
我试过了,但没有得到我需要的结果。例如,我需要 A 组全部一个接一个出现,然后在时间点 15 之后消失。并且需要 B 组全部在时间点 21 之后开始出现,一个接一个地出现,然后在时间点 45 之后消失。
library(ggplot2)
library(gganimate)
library(tibble)
# data
x <- c(-15:-1, -5:19, 1:45, -10:4)
y <- x + rnorm(100, 0, 2) + c(rep(-10, 15), rep(20, 25), rep(-5, 45), rep(8, 15))
id <- c(rep("A", 15), rep("B", 25), rep("C", 45), rep("D", 15))
time <- c(1:15, 21:45, 1:45, 41:55)
tib <- tibble(x,y, id, time)
# does not disappear after one id is done
anim_tib1 <- ggplot(tib, aes(x = x, y = y, col = id, group = id))+
geom_point(size = 2)+
transition_time(time = time)+
exit_disappear()+
shadow_mark()
# this works, but now there is no trail
anim_tib2 <- ggplot(tib, aes(x = x, y = y, col = id, group = id))+
geom_point(size = 2)+
transition_components(time)+
exit_disappear()
# this almost yields the same result as the first try
anim_tib3 <- ggplot(tib, aes(x = x, y = y, col = id, group = id))+
geom_point(size = 2)+
transition_components(time)+
exit_disappear()+
shadow_mark()
我认为 shadow_wake
而不是 shadow_mark
可能会产生您正在寻找的结果:
# modified code from anim_tib1
ggplot(tib, aes(x = x, y = y, col = id, group = id)) +
geom_point(size = 2) +
transition_time(time = time) +
exit_disappear() +
shadow_wake(wake_length = 1, size = NULL, alpha = NULL, wrap = FALSE)
数据:
set.seed(123)
x <- c(-15:-1, -5:19, 1:45, -10:4)
y <- x + rnorm(100, 0, 2) + c(rep(-10, 15), rep(20, 25), rep(-5, 45), rep(8, 15))
id <- c(rep("A", 15), rep("B", 25), rep("C", 45), rep("D", 15))
time <- c(1:15, 21:45, 1:45, 41:55)
tib <- tibble(x, y, id, time)
rm(x, y, id, time)