我正在使用mtcars
数据集(mpg ~ cyl
)绘制回归线(线性)。我使用mpg
和cyl
运行了一个简单的线性模型,并执行了一个总结。scatterplot ggplot2中的斜率和截距问题
线性模型摘要中的截距与图形表示不匹配。
我很难理解发生了什么。如果我使用R的基本绘图函数来绘制散点图,我会得到与ggplot2相同的结果。我改变了Y轴比例限制(0,40),但没有取得任何成功。
这里是我的代码
data(mtcars)
library(ggplot2)
p <- ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point(shape=1) # create graph
p + geom_smooth(method = lm, se=FALSE) # add line
lm.car <- lm(mpg ~ cyl) # create linear model
summary(lm.car) # summary
这里是
> summary(lm.car)
Call:
lm(formula = mpg ~ cyl)
Residuals:
Min 1Q Median 3Q Max
-4.9814 -2.1185 0.2217 1.0717 7.5186
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 37.8846 2.0738 18.27 < 2e-16
cyl -2.8758 0.3224 -8.92 6.11e-10
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.206 on 30 degrees of freedom
Multiple R-squared: 0.7262, Adjusted R-squared: 0.7171
F-statistic: 79.56 on 1 and 30 DF, p-value: 6.113e-10
基于以下几点建议我用
data(mtcars)
library(ggplot2)
p <- ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point(shape=1) +
xlim(0, 10)# create graph
p + geom_smooth(method = lm, se=FALSE) # add line
线性模型输出这里是输出中:
@Julius:感谢您的格式化帮助。 – 2013-03-14 18:47:17