英文:
gganimate for random walk model
问题
我使用ggplot2创建了一个随机漫步图(下面是代码)。我想知道是否可以使用gganimate包,使随机漫步过程(图中的黑线)逐渐出现,但一旦触及灰色水平虚线就停止。
set.seed(3344)
create_random_walk <- function(number=500){
data.frame(x = rnorm(number),
rown = c(1:500)) %>%
mutate(xt = cumsum(x))
}
randomwalkdata <- rbind(mutate(create_random_walk(), run = 1))
p <- ggplot(randomwalkdata, aes(x = rown, y = xt)) +
geom_line() +
labs(x = '时间(任意值)', y = '证据积累\n') +
theme_classic()
p + geom_segment(aes(x = 0.5, xend = 500, y = 25, yend = 25, linetype = 2), colour = "grey", size = 1, show.legend = FALSE) +
scale_linetype_identity()
谁能帮忙吗?
英文:
I have created a random walk plot using ggplot2 (code below). I wondered if it would be possible to use the gganimate package so that the random walk process (the black line in the plot) gradually appears but stops once it touches the grey horizontal dashed line.
set.seed(3344)
create_random_walk <- function(number=500){
data.frame(x = rnorm(number),
rown = c(1:500)) %>%
mutate(xt = cumsum(x))
}
randomwalkdata <- rbind(mutate(create_random_walk(), run = 1))
p <- ggplot(randomwalkdata, aes(x = rown, y = xt)) +
geom_line() +
labs(x = '\nTime (arbitrary value)', y = 'Evidence accumulation\n') +
theme_classic()
p + geom_segment(aes(x = 0.5, xend = 500, y = 25, yend = 25, linetype = 2), colour = "grey", size = 1, show.legend = FALSE) +
scale_linetype_identity()
Can anybody help?
答案1
得分: 2
library(gganimate); library(dplyr)
animate(
ggplot(randomwalkdata |> filter(cumsum(lag(xt, default = 0) >= 25) == 0),
aes(x = rown, y = xt)) +
geom_line() +
geom_point(data = . %>% filter(rown == max(rown)),
size = 10, shape = 21, color = "red", stroke = 2) +
labs(x = '\n时间(任意单位)', y = '证据积累\n') +
theme_classic() +
annotate("segment", x = 0.5, xend = 500, y = 25, yend = 25, linetype = 2,
colour = "grey", linewidth = 1) +
scale_linetype_identity() +
transition_reveal(rown),
end_pause = 20, width = 600)
英文:
library(gganimate); library(dplyr)
animate(
ggplot(randomwalkdata |> filter(cumsum(lag(xt, default = 0) >= 25) == 0),
aes(x = rown, y = xt)) +
geom_line() +
geom_point(data = . %>% filter(rown == max(rown)),
size = 10, shape = 21, color = "red", stroke = 2) +
labs(x = '\nTime (arbitrary value)', y = 'Evidence accumulation\n') +
theme_classic() +
annotate("segment", x = 0.5, xend = 500, y = 25, yend = 25, linetype = 2,
colour = "grey", linewidth = 1) +
scale_linetype_identity() +
transition_reveal(rown),
end_pause = 20, width = 600)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论