I created some sample data...
example.label <- c("A","A","A","A","A","B","B","B","B","B")
example.value <- c(5, 4, 4, 5, 3, 8, 9, 11, 10, 9)
example.age <- c(30, 40, 50, 60, 70, 30, 40, 50, 60, 70)
example.score <- c(90,95,89,91,85,83,88,94,83,90)
example.data <- data.frame(example.label, example.value,example.age,example.score)
Which looks like this...
example.label example.value example.age example.score
1 A 5 30 90
2 A 4 40 95
3 A 4 50 89
4 A 5 60 91
5 A 3 70 85
6 B 8 30 83
7 B 9 40 88
8 B 11 50 94
9 B 10 60 83
10 B 9 70 90
I am able to plot the separate regression lines for 'example.value' based on 'example.label' by doing this...
ggplot(example.data, aes(x=example.age,y=example.value,color=example.label)) +
geom_point() +
geom_smooth(method = lm,fill=NA)
However, this is just producing a simple linear regression with 'method=lm'. The method of plotting I actually want is for each regression line to be calculated with the formula
lm(example.data$example.value ~ example.data$example.age + example.data$example.score)
where 'example.score' is a covariate. Inside of geom_smooth in ggplot, is it possible to adjust that formula used for calculating each line? If so, how would I do that so that it plots the covariate adjusted linear regressions?
EDIT:
Also, to clarify, this is asking how to change the formula that geom_smooth in ggplot2 uses to calculate linear regression from y~x
to y~x+(covariate)
. I am trying to change the way the linear regression is drawn, not how to display its equation on the graph.
I hope this hasn't been asked before. Thanks.