在R中绘制狄拉克和经验分布的混合图

时间:2012-11-18 18:42:26

标签: r ggplot2

我的数据看起来有点像这样:

myData <- data.frame(dist1=rep(0.5, 1000), dist2=rnorm(1000,0.8,0.01), dist3=rnorm(1000,0.7,0.05))

请注意,dist1仅由数字0.5组成。

问题:如何使用R中的ggplot绘制此数据?

我失败的尝试:

如果我尝试geom_density,那么它对dist1不公平:

ggplot(melt(myData), aes(x=value, colour=variable)) + geom_density()

enter image description here

我知道我可以调整内核宽度,但是当dist1变得尖锐时,dist2和dist3开始分解 enter image description here

如果我尝试使用geom_freqpoly,那么它会自动选择bin边界并导致dist1峰值达到0.5的一边,使读者感到困惑,因为它预计会爆炸0.5:

ggplot(melt(myData), aes(x=value, colour=variable)) + geom_freqpoly()

enter image description here

我知道我可以更改bin宽度,但不能更改bin分区本身,否则我会确保在0.5左右等距离的bin分割。

1 个答案:

答案 0 :(得分:0)

我找到了一种使用stat_bin和line geom而不是geom_freqpoly来操作bin的方法。似乎给出了一个改进得很好且可调的结果:

ggplot(melt(myData), aes(x=value, colour=variable)) + 
  stat_bin(origin = 0.491, binwidth = 0.006, geom='line', position='identity')

enter image description here