2014-05-10 101 views
0
> head(d) 
    TargetGroup2012 TargetGroup2000  bmi3 age3 PA_Score education3 asthma3 allasthma3 tres3 
1    2    2 20.89796 55  2   2  0   0  0 
2    2    2 20.20038 49  3   2  0   0  0 
3    2    2 30.47797 58  3   1  0   0  0 
4    2    2 34.13111 51  2   2  0   0  0 
5    3    2 23.24380 52  3   1  0   0  0 
6    3    2 16.76574 62  2   3  0   0  0 
    wheeze3 SmokingGroup_Kai 
1  0    4 
2  1    4 
3  0    5 
4  1    4 
5  0    3 
6  0    3 

分組我做使用gam情節:GGPLOT2情節stat_smooth可變

MyFormula=asthma3~s(bmi3)+s(age3)+PA_Score+eucation3 
c <- ggplot() 
c + stat_smooth(data=d,aes(bmi3,asthma3),method="gam",formula=MyFormula,color="red")+ 
stat_smooth(data=d3,aes(b3,as3),fomula=as3~s(b3))+xlab("BMI3")+ylab("Asthma3") 

我想根據變量TargetGroup2012的價值在同一地塊有不同的線路。

我能獲得這個做的:

d1=d[d$TargetGroup2012==1,] 
d2=d[d$....] 
And then plot... 

是否有快速的方法來做到這一點?也許使用類似groupby的東西?

編輯: 正確的解決方案

d=data[,c("TargetGroup2012","TargetGroup2000","bmi3","age3","PA_Score","education3","asthma3","allasthma3","tres3","wheeze3","SmokingGroup_Kai")] 
d$TargetGroup2012=factor(d$TargetGroup2012) 

d=na.exclude(d) 

ggplot() + stat_smooth(data=d,aes(x=bmi3,y=asthma3,group=TargetGroup2012,color=TargetGroup2012),method="gam",fomula=asthma3~s(bmi3)+s(age3)+PA_Score+SmokingGroup_Kai)+ xlab("BMI3")+ylab("Asthma3") 
+0

您可以在'stat_smooth'函數中使用'group = TargetGroup2012'。 – Jaap

+0

這是部分工作。我想用圖例來獲得不同顏色的線條 – Donbeo

回答

1

要獲得不同顏色的線,你可以用你的color=TargetGroup2012功能stat_smooth裏面。這應該給你想要的結果。