【问题标题】:Regression analysis ggplot2回归分析ggplot2
【发布时间】:2015-08-03 13:09:02
【问题描述】:

我做了多元线性回归。我试图用这个命令来绘制它。

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

之后,我有 4 个图“残差与拟合”、“规模与位置”、“正常 q-q”和“残差与杠杆”。

是否可以使用ggplot2 将 4 个图合二为一?

【问题讨论】:

标签: r ggplot2


【解决方案1】:

解决方案是使用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)

【讨论】:

  • 如何添加主标题?是否可以使用top
  • + ggtitle("你的标题")
  • 如何添加更改平滑方法,因为我偶然发现了这个错误? geom_smooth: method="auto" and size of largest group is &lt;1000, so using loess. Use 'method = x' to change the smoothing method. Error: stat_smooth requires the following missing aesthetics: x, y.
  • 这是两个不同的问题。第一个只是 geom_smooth 正在使用 method=loess 的警告;如果您愿意,可以在 geom_smooth 中更改它,但这有效。第二个意味着您的数据或代码有问题,因为它找不到 x 和 y ;要么您没有在绘图的 aes() 中将任何内容映射到 x 和 y,要么您的数据以某种方式损坏
  • @scoa.我解决了这个问题。谢谢你的好意。我欠你一个。
猜你喜欢
  • 2019-08-17
  • 1970-01-01
  • 1970-01-01
  • 2012-05-23
  • 2018-09-16
  • 1970-01-01
  • 2018-01-06
  • 2021-10-26
  • 2015-12-10
相关资源
最近更新 更多