生成频率直方图以显示每个额外样本后的变化

gganimate a frequency histogram to show change after each additional sample

我有以下数据集代表总体的重复样本和后续样本均值:

data.frame(sample = 1:50, mu = rnorm(n = 50, mean = 0, sd = 1))

我使用以下代码在 ggplot 中制作了频率直方图:

data.frame(sample = 1:50, mu = rnorm(n = 50, mean = 0, sd = 1)) %>% 
ggplot(data = ., aes(mu)) + 
geom_histogram()

我正在尝试使用 gganimate 为这个直方图制作动画,其中每个额外的样本都会改变图中均值的频率分布。但是,我不知道该怎么做。

我试过了,但结果不尽人意:

data.frame(sample = 1:50, mu = rnorm(n = 50, mean = 0, sd = 1)) %>% 
ggplot(data = ., aes(x = mu)) +
  geom_histogram() +
  transition_time(sample) +
  ease_aes("linear") +
  enter_fade() +
  exit_fade()

如有任何帮助,我们将不胜感激。

如果我没理解错的话,您希望直方图随着时间的推移而增加,每一步都添加一个新样本。如果这就是你想要的,可以这样实现:

基本思想是按样本拆分,然后将样本累积到帧中,即在帧1中仅显示样本1,在帧2中显示样本1和2,.....

library(gganimate)
#> Lade nötiges Paket: ggplot2
library(ggplot2)
library(dplyr)
library(purrr)

set.seed(42)

# example data
df <- data.frame(sample = 1:500, mu = rnorm(n = 50, mean = 0, sd = 1))

df_ani <- df %>% 
  split(.$sample) %>% 
  accumulate(~ bind_rows(.x, .y)) %>% 
  bind_rows(.id = "frame") %>% 
  mutate(frame = as.integer(frame))
head(df_ani)
#>   frame sample         mu
#> 1     1      1  1.3709584
#> 2     2      1  1.3709584
#> 3     2      2 -0.5646982
#> 4     3      1  1.3709584
#> 5     3      2 -0.5646982
#> 6     3      3  0.3631284

p_gg <- ggplot(data = df, aes(x = mu)) +
  geom_histogram()
p_gg
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

p_anim <- ggplot(data = df_ani, aes(x = mu)) +
  geom_histogram()

anim <- p_anim + transition_manual(frame) +
  ease_aes("linear") +
  enter_fade() +
  exit_fade()
anim
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

reprex package (v0.3.0)

于 2020-03-17 创建