我正在模拟 40 个独立同分布指数的均值分布,并绘制样本均值的分布以及样本均值 (A) 的分布均值和 lambda = 0.2 (B) 的指数的理论均值。这两种方法 A 和 B 应显示为不同颜色的垂直线,并且应在图例中解释它们的颜色代码。然而,我的代码只产生一条垂直线,并忽略了我在代码中定义的配色方案。
代码如下:
n <- 40
lambda <- 0.2
simulation = data.table(sample_mean = numeric())
for (i in (1 : 1000)){
simulation <- rbind(simulation, data.table(sample_mean = mean(rexp(n, lambda))))
}
#==============================================================================================
# Show the sample mean and compare it to the theoretical mean of the distribution.
#==============================================================================================
sample_mean <- mean(simulation$sample_mean)
4.981267
theoretical_mean <- 1/lambda
5
#------------------------------------------------------------------------------------------
# Plot of the Empirical and Theoretical Distributions and their respective means
#------------------------------------------------------------------------------------------
ggplot(simulation, aes(x = sample_mean) ) +
geom_histogram(aes(y=..density..), position="identity",alpha = 0.4, fill = "red", bins=100) +
geom_density(colour = "red" , size = 2, alpha = 0.5) +
geom_vline(xintercept = sample_mean, aes(colour = "Empirical"), size = 1.5, alpha =0.3) +
geom_vline(xintercept = lambda, aes(colour = "Theoretical"), size = 1.5, alpha =0.3) +
theme_economist() + ggtitle("Distribution of Sample Means. Mean of the Empirical Distribution
and Mean of the Theoretical Exponential (1,000 simulations) ") +
scale_colour_manual("Distributions", values = c("blue", "red")) +
scale_y_continuous(name = "Density") +
scale_x_continuous(name = "Sample Means", breaks = seq(2, 8, .5), limits=c(2, 8))
情节如下:
您的建议将不胜感激。
====================================
编辑
@mkt:感谢您的贡献。我仍然需要在图中注释垂直线,这就是为什么我使用 aes() 中的颜色以及稍后在我的代码中映射到颜色的字符串。所以我仍然需要找到如何做到这一点的解决方案。