r - 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)
解决方案
我认为这里很难使用stat_function
,因为dnorm
您传递的函数包含一个分组变量 ( mean(s)
)。无法表明您希望s
按dataset
列分组,并且该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)
推荐阅读
- c# - var 输出 = 1 + 2 + 3 + “4” + 5 + “90”;输出:64590;;只是想知道为什么?
- reactjs - 有没有内置的方式来加入 sagas
- php - 无需root安装cmark PECL模块
- r - R故障排除脚本,为什么NULL
- java - 是否可以将接口或基类类型作为泛型类型传递给 java.lang.Class
- python - 连接具有不同列数的两个数据帧(垂直)
- ssas - 在 MDX 中实现 IN /LIKE
- raspberry-pi - 在 Raspberrypi3B 上运行 6-7 天后,引导加载程序在 android things os 可启动 sd 卡中消失
- html - 特定 div 中前面的文本和后面的背景图像
- sql - 如何在 SSMS 中格式化长 SQL 查询以使其看起来更有条理?