我尝试在我的ggplot上添加我的(多个)线性回归线。 我早上有两个假人(早上 - 晚上)。 情节是正确的,但是当我想添加回归线时,它会给我一个错误。 这是代码:
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
这是错误:
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
有人知道我做错了什么吗?
答案 0 :(得分:0)
我希望这对你有帮助:
library('ggplot2')
# create test data
weight <- runif(40) * 10
morning <- weight * runif(length(weight))
weight_data <- data.frame(weight, morning)
# test data
ression_1 <- lm( weight ~ poly(morning, degree = 2))
plot(morning, weight)
points(x=morning, y=regression_1$fitted.values, col='red')
#plot in ggplot2
plot2 <- ggplot(data=weight_data, aes(y=weight, x=morning))+
geom_point()+
stat_smooth(method = "lm", formula = y ~ poly(x, degree = 2), size = 1)+
labs(y = "Weight in kg", x = "Day Number of weight measurment", subtitle = "Day 0 = 3 October 2010")
plot2