Roland Roland - 2 months ago 7
R Question

Standardized residuals x Theoretical Quantiles plot in ggplot2 package

I'm trying to do a plot "Theoretical Quantiles"X "Standardized Residuals" using

ggplot2
package.

I have a
lm()
model that I used to this plot

library(ggplot2)

model<-lm(mpg~cyl+disp+hp+drat+wt, data=mtcars)

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()
p2


This code that I founded here https://rpubs.com/therimalaya/43190 make the plot, but returns an error

Error: Aesthetics must be either length 1 or the same as the data (3110): x


and I can't change the
xlab
or
ylab
names. How I can solve it?

Answer
model<-lm(mpg~cyl+disp+hp+drat+wt, data=mtcars)

library(ggplot2)

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()
p2

enter image description here

Comments