2013-04-25 67 views
1

我在GGPLOT2使这个点阵图:如何使平滑密度适合ggplot2的dotplots?

ggplot(mtcars, aes(x = mpg)) + geom_dotplot() 

我想表明某种平滑密度适合在散点图顶部的观察点。这看起来不正确:

ggplot(mtcars, aes(x = mpg)) + geom_dotplot() + geom_density() 

我试图stat_smooth()但我得到的错误:

> ggplot(mtcars, aes(x = mpg)) + geom_dotplot() + stat_smooth() 
stat_bindot: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this. 
Error: stat_smooth requires the following missing aesthetics: y 

感谢。

+0

什么看起来不正确?你意识到密度估计不会与分箱计数的规模相同,对吧? – joran 2013-04-25 21:38:45

+0

@joran:正好。这就是为什么我需要一些适合内核密度的东西来适应实际的点堆,所以它们的尺寸大致相同 – user248237dfsf 2013-04-26 00:08:25

+1

实际上,我认为它仅仅适合一个合适的内核密度估计值,这只是因为你不喜欢结果。看看我的意思,尝试使用'geom_density(adjust = 0.5)'。这里没有真正的“正确”,你只需要修改bin宽度和kde参数,直到你找到一些看起来令人愉悦的东西。 – joran 2013-04-26 01:31:43

回答

0

stat_smooth函数中的平滑公式需要y值,并且由于您没有指定一个值,所以会看到一个错误。

如果指定值的范围为y例如为:

ggplot(mtcars, aes(x = hp, y = mpg)) + geom_dotplot() + stat_smooth() 

编辑错误将消失:

stat_smooth()完美的作品与其他大多数功能。你可能想要的东西(与x和模型Y上MPG)的近似值是:

qplot(mpg, row.names(mtcars), data=mtcars, group=1, color=mpg, xlab="Miles Per Gallon", ylab="Model") + stat_smooth() 

这将导致:

enter image description here

+0

谢谢。代码运行,但它显然是错误的数据适合 – user248237dfsf 2013-04-26 00:08:45

+0

我知道这一点,我只是想证明stat_requires y范围。你能详细说明你想做什么吗?一个快速的qplot非常好地平滑。我将编辑我的答案以演示该功能。 – Abbas 2013-04-27 10:07:16