首页 > 解决方案 > stat_function 没有过渡到 transition_states

问题描述

我正在尝试使用 ggplot2 编写自己的中心极限定理演示,但无法让我的 stat_function 显示变化的正态分布。

下面是我的代码,我希望 stat_function 中的正态分布通过不同的状态进行转换;具体来说,我希望它改变标准偏差以对应数据集中的每个值。任何帮助将不胜感激。

#library defs

library(gganimate)
library(ggplot2)
library(transformr)

#initialization for distribution, rolls, and vectors

k = 2

meanr = 1/k

sdr = 1/k

br = sdr/10

rolls <- 200

avg <- 1

dataset <- 1

s <- 1

#loop through to create vectors of sample statistics from 200 samples of size i

#avg is sample average, s is standard deviations of sample means, and dataset is the indexes to run the transition states

for (i in c(1:40)){

  for (j in 1:rolls){

    avg <- c(avg,mean(rexp(i,k)))

  }

  dataset <- c(dataset, rep(i,rolls))

  s <- c(s,rep(sdr/sqrt(i),rolls))

}


#remove initialized vector information as it was only created to start loops

avg <- avg[-1]

rn <- rn[-1]

dataset <- dataset[-1]

s <- s[-1]


#dataframe 

a <- data.frame(avgf=avg, rnf = rn,datasetf = dataset,sf = s)

#plot histogram, density function, and normal distribution

ggplot(a,aes(x=avg,y=s))+

  geom_histogram(aes(y = ..density..), binwidth = br,fill='beige',col='black')+

  geom_line(aes(y = ..density..,colour = 'Empirical'),lwd=2, stat = 'density') + 

  stat_function(fun = dnorm, aes(colour = 'Normal', y = s),lwd=2,args=list(mean=meanr,sd = mean(s)))+
  
  scale_y_continuous(labels = scales::percent_format()) +

  scale_color_discrete(name = "Densities", labels = c("Empirical", "Normal"))+

  labs(x = 'Sample Average',title = 'Sample Size: {closest_state}')+

  transition_states(dataset,4,4)+ view_follow(fixed_x = TRUE)

标签: r

解决方案


我认为这里很难使用stat_function,因为dnorm您传递的函数包含一个分组变量 ( mean(s))。无法表明您希望sdataset列分组,并且该transition_states函数不会过滤整个数据框。您可以使用transition_filter过滤整个数据框,但这会很费力。

只需将 a 添加dnorm到您的输入数据框并将其绘制为一条线并没有太多工作,特别是因为您的其余代码可以大大简化。这是一个完全可重现的示例:

library(gganimate)
library(ggplot2)
library(transformr)

k     <- 2
meanr <- sdr <- 1/k
br    <- sdr/10
rolls <- 200

a <- do.call(rbind, lapply(1:40, function(i){
            data.frame(avg     = replicate(rolls, mean(rexp(i, k))),
                       dataset = rep(i, rolls),
                       x       = seq(0, 2, length.out = rolls),
                       s       = dnorm(seq(0, 2, length.out = rolls),
                                       meanr, sdr/sqrt(i))) }))

ggplot(a, aes(x = avg, group = dataset)) +
  geom_histogram(aes(y = ..density..), fill = 'beige', 
                 colour = "black", binwidth = br) +
  geom_line(aes(y = ..density.., colour = 'Empirical'), 
            lwd = 2, stat = 'density', alpha = 0.5) +
  geom_line(aes(x = x, y = s, colour = "Normal"), size = 2, alpha = 0.5) +
  scale_y_continuous(labels = scales::percent_format()) +
  coord_cartesian(xlim = c(0, 2)) +
  scale_color_discrete(name = "Densities", labels = c("Empirical", "Normal")) +
  labs(x = 'Sample Average', title = 'Sample Size: {closest_state}') +
  transition_states(dataset, 4, 4) +
  view_follow(fixed_x = TRUE, fixed_y = TRUE)

在此处输入图像描述


推荐阅读