Imagine if my linear regression model was: lm(y~x + N * P) -> (the multiplication is for an interaction)
How would I plot this using ggplot? That all variables are used in the linear regression model. Do I need to use geom_smooth? eventhough the formula in geom_smooth is formula = y~x? does this include the extra variables plus interaction?
I tried:
ggplot(aes(x = x, y = y, color = N) +
geom_point() + geom_smooth(method = 'lm', formula = 'y~x', fullrange = TRUE)
But I have a feeling it does not add all the variables in my ggplot.
ggplot
and afterwards plot the results via ageom_line
, i.e. multiple lines for each or for your desired levels of the interaction terms. See e.g. Add a multivariate linear regression line on a ggplot using R for an example of this approach.y ~ x
finds the best fitting line on a plot of y against x, ignoring your other two variables. If we add a second independent variable by using the formulay ~ x + N
then conceptually we are finding the best fitting plane through a cloud of points in a 3D space with y, x and N on 3 orthogonal axes. This would have to be shown as a 3D plot, or as several lines that are "slices" through this plane. The formulay ~ x + N * P
represents the best fitting 3D space through a 4D point cloud and is very hard to represent on a 2D plot, though using 2 faceting dimensions might work