'Integrating a multiple regression equation (including covariates) into ggplot2 graph

I am currently generating simple linear regression plots in ggplot2 with the following code (toy example)

library(ggplot2)

Data<-data.frame(Age=c(40,41,42,43,44,45,46,47,48,49),
                 Height=c(185,184,183,182,181,186,187,188,189,190),
                 Sex=c("Male","Male","Male","Male","Male","Female","Female","Female","Female","Female"),
                 Weight=c(84,83,82,81,80,85,86,87,88,89),
                 BMI=c(20,21,22,23,24,25,26,27,28,29))

points<-c("#FF9999","turquoise")
lines<-c("dark red","blue")
linetypes<-c(1,1)

x<-ggplot(Data,aes(x=Age,y=Weight))+
  geom_point(aes(fill=factor(Sex)),size=4,alpha=1,shape=21,color="transparent")+
  geom_smooth(aes(color=Sex,linetype=Sex),method="lm",formula=y~x,lwd=1,se=F)+
  scale_fill_manual(values=points,labels=c("Female","Male"))+
  scale_colour_manual(values=lines)+
  scale_linetype_manual(values=linetypes)+
  theme_classic(base_size=15)+
  theme(legend.position="top",legend.direction="horizontal")+
  labs(fill="Points",colour="Lines",x='Age',y='Weight')+
  guides(fill=guide_legend(override.aes=list(shape=c(16,16),alpha=1,size=4,color=c("#FF9999","turquoise"))))+
  guides(colour=guide_legend(override.aes=list(linetype=c(1,1),alpha=1,size=1,color=c("dark red","blue"))))+
  guides(linetype="none")
x

I am stuck as how to add one-or-more covariates, in order to transform the plots from simple to multiple linear regression, e.g., from Weight~Age to Weight~Age+Height+BMI

I am sure that it must have something to do with the method="lm" and formula=y~x commands, but I am not clear on exactly what changes need to be made.

Any help would be very much appreciated. Thank you.

example



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source