2017-07-12 6 views
0

40 i.i.dの平均値の分布をシミュレートしています。 (A)の分布の平均値とラムダ= 0.2(B)の指数関数の理論平均値とをプロットしたものである。 2つの手段AとBは、異なる色の垂直線として表示され、その色コードは凡例に説明する必要があります。しかし、私のコードは1つの垂直線のみを生成し、自分のコード内で定義した配色を無視します。ggplotに縦線が表示されない

コードは以下の通りです:

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

プロット以下の通りです:

enter image description here

あなたのアドバイスは理解されるであろう。

==================================

EDIT

@mkt:ご協力ありがとうございます。それでも、縦線をプロットに注釈を付ける必要があります。これは、後で私のコードで色にマッピングされた文字列でaes()内の色を使用した理由です。だから私はまだそれを行う方法の解決策を見つける必要があります。

+0

だけFYI、アラートをトリガーしませんあなた自身のポストに編集。私はこのページをもう一度チェックしなければ、私の答えを私に見せてコメントする必要があります。 – mkt

+0

明確にするには、以下の解決策に加えて、各行の一番下にラベルを付けるだけですか? – mkt

答えて

0

3つの問題がありました。あなたは、ラムダ、代わりの1をプロットした 1)/ラムダ 2)「実証」と「理論的には、」Rは 3)色がこの作品AES()

内で定義すべきではありませんを認識した色ではありません。

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, colour = "blue", size = 1.5, alpha = 0.3) + 
    geom_vline(xintercept = theoretical_mean, colour = "green", size = 1.5, alpha = 0.5) + 
    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)) 

enter image description here

関連する問題