1

コードは次のとおりです。

dat = data.frame(method=gl(3, 100), res=c(rnorm(100), rnorm(100, 1, 1), rnorm(100, 2, 1)))
png('/tmp/a.png')
p = ggplot(dat)
p = p + stat_density(aes(x=res, group=method, color=as.factor(method)), geom='line')
print(p)
dev.off()

png('/tmp/b.png')
res1 = dat[dat$method==1, ]
res2 = dat[dat$method==2, ]
res3 = dat[dat$method==3, ]
plot(density(res1))
lines(density(res2$res), col='green')
lines(density(res3$res), col='red')
dev.off()

結果:

ここに画像の説明を入力 ここに画像の説明を入力

2 番目の図を使用するplot()と、正しいことがわかります。

4

2 に答える 2

4

デフォルトのstat_density()位置は"stack"- なので、これらの 3 行は積み上げられます。plot()使用中と同じ結果を得るにはposition="identity"

ggplot(dat)+ stat_density(aes(x=res, group=method, color=as.factor(method)), 
         geom='line',position="identity")

ここに画像の説明を入力

于 2013-10-28T14:28:08.367 に答える