I have this working dataset:
chr<-c(1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2)
iid<-c("sc1","sc1","sc2","sc2","sc3","sc3","sc4","sc4","sc5","sc5","sc1","sc2","sc3","sc4","sc5","sc6")
pos1<-c(2,34,7,56,12,67,11,34,2,67,23,56,12,11,12,43)
pos2<-c(23,54,12,98,54,79,22,67,43,98,23,54,65,32,54,57)
fam<-c(1,1,1,1,2,2,2,2,3,3,1,2,3,4,5,6)
data<-data.frame( iid,chr,pos1,pos2,fam)
My intention is to represent the sections between data$pos1 and data$pos2 of each IID. I use the following script to do it by chr:
plot_chr<-function(f,descn,chr){
a<-f[f$chr==chr,]
iids_num<-as.character(unique(a$iid))
nsamps<-length(iids_num)
xlimi<-c(0,max(a$pos2))
plot(NULL,xlim=xlimi,ylim=c(0,nsamps),main=descn,xlab="Physical Position",ylab="Subject")
for (id_no in 1:nsamps) {
plot_dat<-a[which(a$iid==iids_num[id_no]),]
if (length(plot_dat$iid) > 0) {
for (roh_n in 1:length(plot_dat$iid)) {
x<-c(plot_dat[roh_n,"pos1"],plot_dat[roh_n,"pos2"])
y<- c(id_no,id_no)
lines(x,y,lw=2,lend=2,col="red")
}
}
}
return(a)
}
Here is the outcome:
windows()
plot_chr(data,"data",1)
However I want to modify and add a new variable. In that exact plot (chr=1) I would like to change the colors of the lines according to the factor data$fam. For example I would like a red line for iid=sc1 and sc2 (fam=1) a blue line for iid=sc3 and sc4 (fam=2) and green line for iid=sc5 (fam=3). Every time I try to modify the script I end up having errors.

