# [R] plot residuals per factor

arun smartpink111 at yahoo.com
Wed Jan 9 04:03:15 CET 2013

```Hi,
I forgot to mention:
levels(dat1\$d)
# "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
# "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)
# "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)

--
---
Catalin-Constantin ROIBU
Forestry engineer, PhD
Forestry Faculty of Suceava
Str. Universitatii no. 13, Suceava, 720229, Romania
office phone     +4 0230 52 29 78, ext. 531
mobile phone   +4 0745 53 18 01
+4 0766 71 76 58
FAX:                +4 0230 52 16 64
silvic.usv.ro

[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help