Niklaus Hurlimann
2010-May-29 12:31 UTC
[R] plotting density in same plot in loop iteration
Hi R-mailing list I would have the following set-up below with a simplified data-frame. Through a loop which includes certain criteria for the densities I would like to plot the different density-distributions in the same plot. Of course I hope I don't do any mistakes with all the indexes of the dataframe. All I would like to have is the different densities in the same plot with a general background density d. In a second step there might be also the need to combine all the different densities within the loop to one single distribution. I would be grateful for any help on this. Cheers> a[1] 1 4 7 11 16 21 23> b[1] 15 17 13 23 14 11 12> c[1] 0 15 0 0 0 0 0> d[1] 0 0 7 0 16 0 0> e[1] 0 0 0 11 0 21 0> f[1] 0 0 123 124 122 121 0 q<- data.frame(a,b,c,d,e,f) a b c d e f 1 1 15 0 0 0 0 2 4 17 15 0 0 0 3 7 13 0 7 0 123 4 11 23 0 0 11 124 5 16 14 0 16 0 122 6 21 11 0 0 21 121 7 23 12 0 0 0 0 indx<-which(q[,"a"]==q[,"d"]) indy<-which(q[,"a"]==q[,"e"]) s<-[c(indx,indy),] std<-sd(S[,"f"]) t<-round(max(q[,"a"]),0) u<-round(min(q[,"a"]),0) colnames(v)<-c("a") D<-q[,"a"] d<-density(D) pdf('L') for (h in length(s[["a"]])) { for (i in length(S[["a"]])) { inda<-which(q[h,"a"]==q[h,"d"]) indb<-which(q[i,"a"]==q[i,"e"]) R<-(s[c(inda),"f"]+s[c(indb),"f"])/2 indh<-which(abs(q[,"b"]-R)<=(2*std)) indi<-which(abs(q[,"c"]-R)<=(2*std)) A<-q[indh,c("a","b")] colnames(A)<- c("a","g") B<-q[indy,c("a","c")] colnames(B)<- c("a","g") C<-rbind(A,B) c<-density(C[,"a"]) plot(d, col="blue",xlim=c(u,t),ylim=c(0,0.5)) polygon(d, col="blue",border="blue") lines(c, col="red") } } dev.off() -- Niklaus Hürlimann Université de Lausanne Institut de Minéralogie et Géochimie L'Anthropole CH-1015 Lausanne Suisse E-mail: Niklaus.Hurlimann@unil.ch Tel:+41(0)21 692 4452 [[alternative HTML version deleted]]