简体   繁体   English

在ggplot中添加多个线性回归线时出错

[英]Error in adding multiple linear regression lines in ggplot

I try to add my (multiple) linear regression lines on my ggplot. 我尝试在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? 有人知道我做错了什么吗?

I hope this still help you: 希望这对您有所帮助:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM