2013-04-25 45 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