回归分析 ggplot2

3

我已经完成了多元线性回归。我尝试用这个命令绘制它。

layout(matrix(c(1,2,3,4),2,2)) 
plot(fit_ec_urban_franchise)

接下来我有4个图形:'残差-拟合值图','标准化残差-拟合值图','正态 Q-Q 图' 和 '残差杠杆值图'。

是否可以使用ggplot2将这4个图形合并成一个图形呢?


寻找具有autoplot.lm函数的软件包,例如http://www.inside-r.org/packages/cran/eeptools/docs/autoplot.lm。 - scoa
谢谢你的帮助。你知道另外一种方法吗? - jjjjjjj jjjjjjj
1个回答

4
一种解决方案是使用ggplot2::fortify。以下是在其帮助页面上可以找到的代码?fortify。我添加了gridExtra以将4个图形排列在一起。
library(ggplot2)
library(gridExtra)

mod <- lm(mpg ~ wt + cyl, data = mtcars)

p1 <- qplot(.fitted, .resid, data = mod) +
  geom_hline(yintercept = 0) +
  geom_smooth(se = FALSE)

p2 <- qplot(sample =.stdresid, data = mod, stat = "qq") + geom_abline()

p3 <- qplot(.fitted, sqrt(abs(.stdresid)), data = mod) + geom_smooth(se = FALSE)

p4 <- qplot(.hat, .stdresid, data = mod) + geom_smooth(se = FALSE)


grid.arrange(p1,p2,p3,p4)

enter image description here


我该如何添加一个主标题?可以使用“top”吗? - jjjjjjj jjjjjjj
  • ggtitle("在此输入您的标题")
- scoa
我如何添加更改平滑方法,因为我遇到了这个错误?geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method. Error: stat_smooth requires the following missing aesthetics: x, y. - jjjjjjj jjjjjjj
这是两个不同的问题。第一个只是一个警告,说明geom_smooth正在使用method=loess;如果需要,请在geom_smooth中更改它,但这样可以工作。第二个意味着您的数据或代码存在问题,因为它找不到x和y;要么您没有将任何内容映射到绘图的aes()中的x和y,要么您的数据出现了某些损坏。 - scoa
@scoa。我解决了这个问题。谢谢你的好意。我欠你一个人情。 - jjjjjjj jjjjjjj

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接