[R] add lines to a plot with a loop without erase the last one

Duncan Murdoch murdoch at stats.uwo.ca
Sat Apr 8 13:40:09 CEST 2006


On 4/8/2006 7:26 AM, Stephanie d'Agata wrote:
> Hello,
> 
> I want to plot several lines to a main plot but at the end of each loop, it
> erases the last one.

A call to plot() will normally erase what was there and start over 
again.  If you want to add lines, then use the lines() function in your 
loop.

A complication is that you have 3 plots, and it looks as though you want 
to draw all of them gradually (rather than drawing one completely, then 
drawing the next one). This is tricky in a case like yours where the 
plots have different scales.  I recommend that you rearrange your 
program so that you can complete one figure before you move on to the next.

Duncan Murdoch

> Here my program:
> 
> 	alpha<-5
> 	beta<-10
> 	var<-0.5
> 
> 	s<-runif(1000,0,50)
> 	m<-length(s)
> 
> 
> 	variancealpha<-0.002
> 	variancebeta<-0.051
> 	variancevar<-0.001
> 
> 	alphachap<-vector()
> 	betachap<-vector()
> 	varchap<-vector()
> 	epsilon<-vector()
> 	b<-vector()
> 	u<-vector()
> 	varbeta<-vector()
> 	varalpha<-vector()
> 	biaisbeta<-vector()
> 	biaisalpha<-vector()
> 	biaisvar<-vector()
> 
> 	data<-matrix(99,length(s),n)
> 
> 
> # III) Je génère les erreurs suivant une loi normale centrée sur 0 et de
> variance sigma² déjà fixée.
> 
> d<-c(5,10,20,100,200,400,450,500,800)
> 
> for (k in 1:length(d))
> 
> {
> 
> 	n<-d[k]
> 	print(n)
> 		for (i in 1:n)
> 		{
> 
> 			epsilon<-rnorm(length(s),0,sqrt(var))
> 
> 
> 				for (j in 1:(length(s)))
> 				{
> 
> 
> 
> 					data[j,i]<-((alpha*s[j]) / (s[j] + beta)) + (epsilon[j]*(s[j]/(s[j] +
> beta)))
> 
> 					b<-data[,i]
> 
> 				}
> 
> #print(b)
> X<-b[-1]/s[-1]
> #print(X)
> #print(X)
> #print(length(b[-1]))
> #print(length(X))
> meanX<-mean(X)
> #print(meanX)
> meanV<-mean(b)
> #print(meanV)
> 
> betachap[i]<-((meanX*squareproduct(b[-1],b[-1]))-
> (meanV*squareproduct(X,b[-1])))/((meanV*squareproduct(X,X))-
> (meanX*squareproduct(X,b[-1])))
> alphachap[i]<-meanV + betachap[i]*meanX
> varchap[i]<-((squareproduct(b[-1],b[-1])) +
> (2*betachap[i]*squareproduct(X,b[-1])) +
> (((betachap[i]^2)*squareproduct(X,X))))/(length(s)-2)
> 
> 
> #u<-(alpha/(s+beta))
> 
> 
> #varbeta<-var/(1+ (2*var/(alpha^2))*(squareproduct(u,u))/(alpha^2))
> #varalpha<-(var/m) + (mean(u)^2)*(varbeta)
> 
> 
> 		}
> 
> #print(data)
> #print(alphachap)
> #print(betachap)
> #print(varchap)
> #print(varbeta)
> #print(varalpha)
> 
> 
> 
> biaisbeta[k]<-bq(betachap,beta)
> biaisalpha[k]<-bq(alphachap,alpha)
> biaisvar[k]<-bq(varchap,var)
> 
> par(mfrow=c(3,1))
> 
> plot(seq(5,15,0.01),dnorm(seq(5,15,0.01),10,sqrt(variancebeta)),col=2,type="
> l")
> lines(density(betachap))
> 
> plot(seq(2.5,7.5,0.01),dnorm(seq(2.5,7.5,0.01),5,sqrt(variancealpha)),col=2,
> type="l")
> lines(density(alphachap))
> 
> plot(seq(0,1,0.01),dnorm(seq(0,1,0.01),0.5,sqrt(variancevar)),col=2,type="l"
> )
> lines(density(varchap))
> 
> 
> }
> 
> 
> 
> 
> Thanks.
> 
> steph
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list