I try to add my (multiple) linear regression lines on my ggplot. I have two dummies (morning-evening) for the morning.dummy. The plot is correct but it gives me an error when I want to add the regression lines. Here is the code:
regression_1 <- lm(weight ~ morning.dummy + dayNumber + (morning.dummy*dayNumber) +
I(dayNumber^2) + (I(dayNumber^2)*morning.dummy),
data=weight_data)
summary(regression_1)
#plot
plot2 <- ggplot(data=weight_data,aes(x=dayNumber, y=weight, color=morning.dummy)) +
geom_point()+
stat_smooth(method = "lm", formula = weight ~ morning.dummy + dayNumber + (morning.dummy*dayNumber) +I(dayNumber^2) + (I(dayNumber^2)*morning.dummy), size = 1) +
labs(y = "Weight in kg", x = "Day Number of weight measurment", subtitle = "Day 0 = 3 October 2010")
plot2
This is the error:
Error in grid.Call.graphics(C_setviewport, vp, TRUE) :
non-finite location and/or size for viewport
In addition: Warning message:
Computation failed in `stat_smooth()`:
object 'dayNumber' not found
Does someone have an idea where I do something wrong?
stat_smooth(method = "lm", formula = weight ~ poly(daynumber, degree = 2), size = 1)
should give the same prediction as your model. - Rolandstat_smooth(method = "lm", formula = y ~ poly(x, degree = 2), size = 1)
. See here: stackoverflow.com/a/25031125/1412059 - Rolandy ~ poly(x1, degree = 2) * x2
should be the same as predictions from separate polynomial fits for each factor level as ggplot2 would fit. But of course, one could also easily calculate predictions outside ggplot2 and pass these to the ggplot. - Roland