'How do I plot two regression lines on the same plot with different x and y?
I am required to fit two simple linear regression lines, one with "y = father" and "x = son", the other with "y = son" and "x = father". I was able to do this with no issues and have gathered the correct equations. However, I am also required to plot them on the same scatterplot which is where I am running into some trouble. I am curious if there is a way I can plot the "y = father" and "x = son" regression line onto the scatterplot where y = son and x = father. Or is there a way I can combine the following two plots?
ggplot(galton_heights, aes(x = father, y = son)) +
geom_point() +
geom_abline(slope = 0.46139, intercept = 37.28761, col = "blue") +
theme_bw()
ggplot(galton_heights, aes(x = son, y = father)) +
geom_point() +
geom_abline(slope = 0.40713, intercept = 40.93831, col = "red") +
coord_flip() +
theme_bw()
I was told my plot should look similar to this which is the two separate graphs I have above combined together.
Solution 1:[1]
for the first part, you can simply use the geom_smooth to draw a linear regression line:
ggplot(galton_heights, aes(x = father, y = son)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
theme_bw
For the second part of the question, it doesn't really make sense to do it, as you should change the axis. You can do that:
ggplot(galton_heights, aes(x = father, y = son)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, col = "blue") +
geom_smooth(aes(y = father, x = son), method = "lm", se = FALSE, col = "red") +
theme_bw()
But it is theoretically wrong.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bloxx |

