0

I'm doing a dose-response plot where the dietary intake (x-axis) divides in two intervention groups (high protein vs. low protein) and y-axis shows the change in a bone outcome.

"x" is defined from a variable in my dataset called "Protein", where the factor "1" is high protein and "2" is low protein

I want to do a linear regression line showing the association between intake and change in bone outcome, however, since the assumption of normal distribution is not true, I would like to do two separate lines — one for each group (high vs. low) — showing the association.

I have used the following code:

abline(lm(y ~ x:factor(Protein) + factor(Protein) -1, data = CC_compliance.long), col = "blue")

However, the plot only shows one line, which doesn't make any sense:

Dose-response plot with only one linear regression line

I'm thinking that it may have something to do with the code not being able to differentiate between Protein=1 and Protein=2. Can i define this in another way?

Dataset:

structure(list(Protein = c(1, 2, 2, 1, 2, 2, 2, 1, 1, 2), Change_bone = c(72.1, 132.2, 57.8, 55.6, 30.4, 49.1, 46.8, 24.6, 44.8, 96.4), Protein_intake = c(20.9, 11.2, 7.9, 17.5, 10, 1.5, 9.3, 20, 22.5, 10.4)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))
  • 1
    Please add a [reproducible example](https://stackoverflow.com/a/5963610/11570343). This is what you [want?](https://stackoverflow.com/a/23725971/11570343). – cdcarrion Sep 15 '20 at 16:00
  • I added a snippet of my dataset from Excel as an image to the post. This is my first Stackoverflow post, so I'm not sure what else to add or what you need. – Camilla Friis Bryde Nielsen Sep 15 '20 at 16:17
  • 2
    Instead of showing a screenshot of your data, please use `dput()` or one of the approaches mentioned [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to put your data in your question in a more convenient form ... – Ben Bolker Sep 15 '20 at 16:20
  • I added a structure to my post now. – Camilla Friis Bryde Nielsen Sep 15 '20 at 16:43

1 Answers1

0

You can use ggplot like here:

ggplot() +
  geom_point(data = df, aes(x = Protein_intake, y = Change_bone)) +
  geom_smooth(data = df[df$Protein == 1, ], 
              aes(x = Protein_intake, y = Change_bone, color = "1"),
              method = "lm",
              se = FALSE) +
  geom_smooth(data = df[df$Protein == 2, ],
              aes(x = Protein_intake, y = Change_bone, color = "2"),
              method = "lm", 
              se = FALSE) +
  scale_colour_manual(name="legend", values=c("blue", "red"))


Or with graphics you may use:

plot(df$Protein_intake, df$Change_bone) +
abline(reg = lm(Change_bone ~ Protein_intake , data = df[df$Protein == 1,]), col = "blue") +
abline(lm(Change_bone ~ Protein_intake , data = df[df$Protein == 2,]), col = "red")  

tamtam
  • 3,541
  • 1
  • 7
  • 21
  • I tried the ggplot with my own dataset and got the error: "Error: Aesthetics must be either length 1 or the same as the data (85): x and y" Then i tried the graphics and got the error: "Error: need finite 'ylim' values" – Camilla Friis Bryde Nielsen Sep 15 '20 at 17:12