2012-01-19 27 views
2

當我做一個簡單的qplot()時,我想要一條迴歸線。我如何告訴stat_smooth忽略這些因素?當變量被分解時,gplot2中的Qplot會產生多條迴歸線

這裏是我的示例代碼:

library("ggplot2") 
qplot(y=wt, x=mpg, size=cyl, col=factor(gear), data=mtcars) + 
    stat_smooth(method=lm, formula=y~x) 

這給出了這樣的形象:

A factored smooth line

當我刪除的因素,我得到我想要的(雖然我不能刪除該圖因子在我的真實數據集中):

qplot(y=wt, x=mpg, size=cyl, col=gear, data=mtcars) + 
    stat_smooth(method=lm, formula=y~x) 

A proper image

回答

8

您可以將點(您希望通過循環和齒輪系數對其進行分組)從平滑器(爲此只需要x和y美學,而沒有其他)分開。

ggplot(mtcars, aes(y=wt, x=mpg)) + 
    geom_point(aes(size=cyl, colour=factor(gear))) + 
    stat_smooth(method="lm") 

或者,如果你有很多GEOM,並希望只從一個GEOM刪除默認的美學:

ggplot(mtcars, aes(y=wt, x=mpg, size=cyl, colour=factor(gear))) + 
    geom_point() + 
    stat_smooth(method="lm", aes(size = NULL, colour = NULL)) 
+0

順便說一下你對如何做的傳說點的任何想法使用尺寸= cyle參數時齒輪顏色變大?點變得非常小。我已經看過所有的legend.key opts(),但我找不到任何有用的東西 –

相關問題