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,320
  • 1
  • 42
  • 64
  • 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
  • 122,144
  • 10
  • 182
  • 276