gganimate用于随机游走模型

huangapple go评论78阅读模式
英文:

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()

gganimate用于随机游走模型

谁能帮忙吗?

英文:

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 &lt;- function(number=500){
  data.frame(x = rnorm(number),
             rown = c(1:500)) %&gt;%
    mutate(xt = cumsum(x))
}

randomwalkdata &lt;- rbind(mutate(create_random_walk(), run = 1))

p &lt;- ggplot(randomwalkdata, aes(x = rown, y = xt)) + 
  geom_line() +
  labs(x = &#39;\nTime (arbitrary value)&#39;, y = &#39;Evidence accumulation\n&#39;) +
  theme_classic()

p + geom_segment(aes(x = 0.5, xend = 500, y = 25, yend = 25, linetype = 2), colour = &quot;grey&quot;, size = 1, show.legend = FALSE) +
  scale_linetype_identity()

gganimate用于随机游走模型

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)

gganimate用于随机游走模型

英文:
library(gganimate); library(dplyr)
animate(
  ggplot(randomwalkdata |&gt; filter(cumsum(lag(xt, default = 0) &gt;= 25) == 0), 
       aes(x = rown, y = xt)) + 
  geom_line() +
  geom_point(data = . %&gt;% filter(rown == max(rown)), 
         size = 10, shape = 21, color = &quot;red&quot;, stroke = 2) +
  labs(x = &#39;\nTime (arbitrary value)&#39;, y = &#39;Evidence accumulation\n&#39;) +
  theme_classic() +
  annotate(&quot;segment&quot;, x = 0.5, xend = 500, y = 25, yend = 25, linetype = 2, 
               colour = &quot;grey&quot;, linewidth = 1) +
  scale_linetype_identity() +
  transition_reveal(rown), 
  end_pause = 20, width = 600)

gganimate用于随机游走模型

huangapple
  • 本文由 发表于 2023年2月14日 01:36:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439364.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定