Skip to content
Prev 314886 / 398506 Next

plot residuals per factor

Hi,
I forgot to mention:
levels(dat1$d)
#[1] "1" "2" "3" "4" "5"

Suppose, if I use different levels
library(car)
dat1$d1<-recode(dat1$d,"1='A';2='B';3='C';4='D';5='E'")
?levels(dat1$d1) # check the order of the levels
#[1] "A" "B" "C" "D" "E"
mypath<-file.path("/home/arun/Trial1",paste("catalin_",LETTERS[1:5],".jpg",sep=""))? #change the file.path according to your system 

for(i in seq_along(mypath)){
?jpeg(file=mypath[i])
?par(mfrow=c(2,2))
?line<-lm(y~x,data=dat1[as.numeric(dat1$d1)==i,])
? plot(line,which=1:4)# if you want only residual vs. fitted, change which=1
? #abline(0,0)
? dev.off()
? } 

In case you need to change the order of levels
?dat1$d1<-factor(dat1$d1,levels=c("C","D","E","A","B"))
?levels(dat1$d1)
#[1] "C" "D" "E" "A" "B"

mypath<-file.path("/home/arun/Trial1",paste("catalin_",LETTERS[c(3,4,5,1,2)],".jpg",sep=""))
?for(i in seq_along(mypath)){
?jpeg(file=mypath[i])
? par(mfrow=c(2,2))
? line<-lm(y~x,data=dat1[as.numeric(dat1$d1)==i,])
?? plot(line,which=1:4)
?? #abline(0,0)
?? dev.off()
?? } 
A.K.







----- Original Message -----
From: catalin roibu <catalinroibu at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Tuesday, January 8, 2013 4:22 AM
Subject: [R] plot residuals per factor

Dear R-users,
I want to plot residuals vs fitted for multiple groups with ggplot2.
I try this code, but unsuccessful.
library("plyr")
models<-dlply(dat1,"d",function(df)
mod<-lm(y~x,data=df)

? ggplot(models,aes(.fitted,.resid), color=factor(d))+
? geom_hline(yintercept=0,col="white",size=2)+
? geom_point()+
? geom_smooth(se=F)