2014-01-06 4 views
1

다음 코드를 사용하여 두 벡터 Model.1과 Model.2에서 두 개의 히스토그램을 얻습니다. 나는 R을 각각의 히스토그램에 대한 정규 곡선에 맞도록하고 싶습니다.히스토그램에 일반 곡선 맞추기

library(ggplot2) 

require(ggplot2) 
require(reshape2) 

set.seed(1) 
df <- data.frame(x<-rnorm(1000,mean = 0.5, sd = 0.01), 
       y<-rnorm(1000,mean = 0.5, sd = 0.01)) 
df2 = melt(df) 

ggplot(df2, aes(value, fill = variable)) + geom_histogram(position = "dodge", binwidth=0.001,colour = "black")+ scale_fill_manual(values = c('red','blue'))+ 
    facet_wrap(~variable, nrow=2)+theme_bw()+ scale_x_continuous(breaks=seq(0.45,0.540,1/200))+ geom_vline(xintercept = 0.5, colour="black") + 
    stat_function(fun=dnorm) 

감사합니다.

+1

'dput (df2)'의 출력을 줄 수있어 우리가 데이터를 다시 만들 수 있습니까? –

+2

이것은 많이 묻습니다. 히스토그램에 정규 곡선을 맞추는 것은 데이터의 표본 평균 및 표준 dev를 계산하는 것입니다 (이것은 실제 평균 및 표준 dev의 추정치가 좋기 때문에). 음모에 일반 곡선이 겹쳐 지길 원한다는 뜻인가요? –

+0

[이것은 좋은 대답 인 것 같습니다.] (http://stackoverflow.com/questions/7182556/how-to-add-gaussian-curve-to-histogram-created-with-qplot) [여기 또 다른 좋은 링크입니다 :] (http://stackoverflow.com/questions/1376967/using-stat-function-and-facet-wrap-together-in-ggplot2-in-r) – marbel

답변

2

유용한 링크 : ggplot와

: 기본 그래픽으로 How to add gaussian curve to histogram created with qplot?

using stat_function and facet_wrap together in GGPLOT2 in R

: Fitting a density curve to a histogram in R

여기 문제에 Andreis의 답변입니다.

library(ggplot2) 
data <- data.frame(V1 <- rnorm(700), V2=sample(LETTERS[1:7], 700, replace=TRUE)) 
ggplot(data, aes(x=V1)) + 
    stat_bin(aes(y=..density..)) + 
    stat_function(fun=dnorm) + 
    facet_grid(V2~.) 

이것은 내가 시도한 것이지만 실제로 작동하지는 않습니다. 아마도 ggplot2 전문가가 우리를 iluminate 할 수 있습니다. :)

ggplot(df2, aes(x = value)) + 
    stat_bin(aes(y =..density..)) + 
    stat_function(fun = dnorm) + 
    facet_grid(. ~ variable) 


ggplot(data = df2, aes(x = value)) + 
    facet_wrap(~ variable) + 
    geom_histogram(aes(y = ..density..)) + 
    stat_function(fun = dnorm, colour = "blue") 
+0

현재 코드에서이 작업을 수행 할 수없는 것처럼 보였습니다. 정규 분포를 피팅하는 것과 관련된 다른 질문을 살펴 보았습니다. 그것은 나의 모범에서 어떻게 정확하게 보이나요? 감사. –

+0

@ Ku-trala 당신은 결과에 대해 당신이 원하는 것과 실제로 얻은 것을 이해할 수 있도록 "작동하지 않는다"에 대해 매우 구체적이어야합니다. –

관련 문제