1
> 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

I am doing a gam plot using:

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")

I would like to have different lines on the same plot according to the value of the variable TargetGroup2012.

I can obtain this doing:

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

Is there a faster way to do that? Maybe using something like groupby?

EDIT : Correct solution

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")
2
  • You can use group=TargetGroup2012 inside your stat_smooth function. Commented May 10, 2014 at 13:55
  • That is partially working. I would like to obtain lines of different color with a legend Commented May 10, 2014 at 15:00

1 Answer 1

2

To get lines with different colors, you can use color=TargetGroup2012 inside your stat_smooth function. That should give you the desired result.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.