0

I am planning to use R for 9 plots on residual vs 9 prediction variables from multiple linear regression. I know that I can write 9 lines of plot function for each of the 9 variables, but I believe there is an elegant way to plot them with a short loop. (let's say y and x1... x9)

Naive way:

plot(x1, residual); plot(x2, residual).....

Is there a better way of doing this?

nograpes
  • 18,623
  • 1
  • 44
  • 67
  • Welcome to Stack Overflow! Please consider providing some example data and the code you've used so far on those data in order to make this [a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Thomas Mar 01 '14 at 14:41

1 Answers1

2

Here is an example using ggplot2:

fit <- lm(Sepal.Length ~ Petal.Length + Petal.Width, data=iris)

iris$resid <- residuals(fit)
library(reshape2)
plotDF <- melt(iris[, c("Petal.Length", "Petal.Width", "resid")], id="resid")

library(ggplot2)
ggplot(plotDF, aes(x=value, y=resid)) + 
  geom_point() + facet_wrap(~ variable)
Roland
  • 127,288
  • 10
  • 191
  • 288
  • @user3368404 If that's the case you should consider ticking the check-mark at the upper left of the answer and mybe also give it a vote. – Roland Mar 04 '14 at 07:50