0

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)

Which looks like this...Graph

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.

David
  • 259
  • 1
  • 2
  • 8
  • If you are treating `age` and `score` as continuous variables the how do you think the 2D plot should capture 3 variables and their relationship? Do you think you'll see a line or something else? – A Gore Jun 26 '17 at 19:05
  • What I really want to do is just change the formula that geom_smooth uses from y~x to y~x+(covariate), which in my actual data has a useful relationship, but I cannot figure out how to display it. The 2D plot of the data points should look the same, but the regression lines should look different since they would then have a covariate. – David Jun 27 '17 at 14:13
  • The values of y will then depend on not only x but also the other covariate. Hence you can fix the covariate to a specific value and show the relation between y and x in which case you'll have to use `geom_abline` – A Gore Jun 27 '17 at 14:17
  • @AGore What do you mean by 'fix the covariate to a specific value'? Also, does geom_abline account for the two separate populations that need a regression line? – David Jun 27 '17 at 15:22

0 Answers0