最新版本的ggplot2用aes参数创建问题

Nis*_*ant 5 r ggplot2

我创建了一个自定义函数来绘制回归诊断,就像这些版本的ggplot2和gridextra一样:

ggplot2        * 1.0.1      2015-03-17 CRAN (R 3.2.1)                
gridExtra      * 2.0.0      2015-07-14 CRAN (R 3.2.1)

head(dadHospital)
      SL. BODY.WEIGHT TOTAL.COST.TO.HOSPITAL
## 1   1          49                 660293
## 2   2          41                 809130
## 3   3          47                 362231
## 4   4          80                 629990
## 5   5          58                 444876
## 6   6          45                 372357

fit1<-lm(TOTAL.COST.TO.HOSPITAL~BODY.WEIGHT,data=dadHospital)   

#custom function of plotting model diagnostics using ggplot2

library(ggplot2)
diagPlot<-function(model){
p1<-ggplot(model, aes(.fitted, .resid))+geom_point()
p1<-p1+stat_smooth(method="loess")+geom_hline(yintercept=0, col="red", linetype="dashed")
p1<-p1+xlab("Fitted values")+ylab("Residuals")
p1<-p1+ggtitle("Residual vs Fitted Plot")+theme_bw()

p2<-ggplot(model, aes(qqnorm(.stdresid)[[1]], .stdresid))+geom_point(na.rm = TRUE)
p2<-p2+geom_abline(aes(qqline(.stdresid)))+xlab("Theoretical Quantiles")+ylab("Standardized Residuals")
p2<-p2+ggtitle("Normal Q-Q")+theme_bw()

p3<-ggplot(model, aes(.fitted, sqrt(abs(.stdresid))))+geom_point(na.rm=TRUE)
p3<-p3+stat_smooth(method="loess", na.rm = TRUE)+xlab("Fitted Value")
p3<-p3+ylab(expression(sqrt("|Standardized residuals|")))
p3<-p3+ggtitle("Scale-Location")+theme_bw()

p4<-ggplot(model, aes(seq_along(.cooksd), .cooksd))+geom_bar(stat="identity", position="identity")
p4<-p4+xlab("Obs. Number")+ylab("Cook's distance")
p4<-p4+ggtitle("Cook's distance")+theme_bw()

p5<-ggplot(model, aes(.hat, .stdresid))+geom_point(aes(size=.cooksd), na.rm=TRUE)
p5<-p5+stat_smooth(method="loess", na.rm=TRUE)
p5<-p5+xlab("Leverage")+ylab("Standardized Residuals")
p5<-p5+ggtitle("Residual vs Leverage Plot")
p5<-p5+scale_size_continuous("Cook's Distance", range=c(1,5))
p5<-p5+theme_bw()+theme(legend.position="bottom")

p6<-ggplot(model, aes(.hat, .cooksd))+geom_point(na.rm=TRUE)+stat_smooth(method="loess", na.rm=TRUE)
p6<-p6+xlab("Leverage hii")+ylab("Cook's Distance")
p6<-p6+ggtitle("Cook's dist vs Leverage hii/(1-hii)")
p6<-p6+geom_abline(slope=seq(0,3,0.5), color="gray", linetype="dashed")
p6<-p6+theme_bw()

return(list(rvfPlot=p1, qqPlot=p2, sclLocPlot=p3, cdPlot=p4, rvlevPlot=p5, cvlPlot=p6))
}

par(mfrow=c(1,1))
diagPlts<-diagPlot(fit1)

#To display the plots in a grid, some packages mentioned above should be installed.

library(gridExtra)
grid.arrange(diagPlts$rvfPlot,diagPlts$qqPlot,diagPlts$sclLocPlot,diagPlts$cdPlot,diagPlts$rvlevPlot,diagPlts$cvlPlot,ncol=3)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

现在使用最新版本ggplot2 2.0.0如果我运行相同的功能,我得到这个错误:

Error: Aesthetics must be either length 1 or the same as the data (248): x
Run Code Online (Sandbox Code Playgroud)

我需要一些帮助.我假设最新ggplot2版本已经引入了一些变化aes,我不知道.

While debugging i get the error here....

p2<-ggplot(fit1, aes(qqnorm(.stdresid)[[1]], .stdresid))+geom_point(na.rm = TRUE)
p2<-p2+geom_abline(aes(qqline(.stdresid)))+xlab("Theoretical Quantiles")+ylab("Standardized Residuals")
p2<-p2+ggtitle("Normal Q-Q")+theme_bw()
p2
Error: Aesthetics must be either length 1 or the same as the data (248): x
Run Code Online (Sandbox Code Playgroud)

Did*_*rts 4

您可以将代码替换为具有相同情节的p2特殊代码。geom_qq()

 p2<-ggplot(model,aes(sample=.stdresid))+geom_qq()
 p2<-geom_abline()+xlab("Theoretical Quantiles")+ylab("Standardized Residuals")
 p2<-p2+ggtitle("Normal Q-Q")+theme_bw()
Run Code Online (Sandbox Code Playgroud)

对于您现有的代码,您只需aes()geom_abline().

  p2<-ggplot(model, aes(qqnorm(.stdresid)[[1]], .stdresid))+geom_point(na.rm = TRUE)
  p2<-p2+geom_abline()+xlab("Theoretical Quantiles")+ylab("Standardized Residuals")
  p2<-p2+ggtitle("Normal Q-Q")+theme_bw()
Run Code Online (Sandbox Code Playgroud)