Plotting multiple regression lines based on a variable in R ggplot2 and also using a covariate?
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)
What 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 can plot 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 just creates a simple linear regression using the 'method = lm' method. The plotting method that I really want for each regression line is calculated using the formula
lm(example.data$example.value ~ example.data$example.age + example.data$example.score)
where "example.score" is a covariate. Inside geom_smooth in ggplot can this formula be used to calculate each row? If so, how would I go about it so that it defines corrected linear regressions with covariance?
EDIT: Also, to clarify this, you are asking how to change the formula that geom_smooth in ggplot2 uses to calculate the linear regression from y~x
to y~x+(covariate)
. I am trying to change the way linear regression is plotted, not how to display its equation on a graph.
I hope this was not asked again. Thank.
source to share
No one has answered this question yet
See similar questions:
or similar: