title: Position of the legend
body: <p>i would like to write a function with graphical output of original data regression and one for modified data. The original data regression should be an option. Moreover there should be legends in the graphs. And here is my problem:
  If i choose the option: orig.plot=FALSE, everything works ok.
  But when i choose the other option: orig.plot=TRUE, the position of my legends is not very satisfying.</p>
  
  <pre><code># Generation of the data set
  
  set.seed(444)
  
  nr.outlier&lt;- 10
  
  x&lt;-seq(0,60,length=150);
  y&lt;-rnorm(150,0,10);
  yy&lt;-x+y;
  d&lt;-cbind(x,yy)
  
  
  # Manipulation of data:
  ss1&lt;-sample(1:nr.outlier,1) # sample size 1
  sri1&lt;-sample(c(1:round(0.2*length(x))),ss1) # sample row index 1
  sb1&lt;-c(yy[quantile(yy,0.95)&lt;yy])# sample base 1
  d[sri1,2]&lt;-sample(sb1,ss1,replace=T) # manipulation of part 1
  
  ss2&lt;-nr.outlier-ss1 # sample size 2 
  sri2&lt;-sample(c(round(0.8*length(x)+1):length(x)),ss2) # sample row index 2
  sb2&lt;-c(yy[quantile(yy,0.05)&gt;yy])# sample base 2
  d[sri2,2]&lt;-sample(sb2,ss2,replace=T) # manipulation of par 2
  
  
  
  tlm2&lt;-function(x,y,alpha=0.95,orig.plot=FALSE,orig.ret=FALSE){
  
  m1&lt;-lm(y~x)
  res&lt;-abs(m1$res)
  topres&lt;-sort(res,decreasing=TRUE)[1:round((1-alpha)*length(x))] # top alpha*n residuals
  topind&lt;-rownames(as.data.frame(topres)) # indices of the top residuals
  x2&lt;-x[-as.numeric(topind)] #
  y2&lt;-y[-as.numeric(topind)] # removal of the identified observations
  
  m2&lt;-lm(y2~x2)
  r2_m1&lt;-summary(m1)$'r.squared'
  r2_m2&lt;-summary(m2)$'r.squared'
  if(orig.plot==TRUE){
      par(mfrow=c(2,1))
      plot(x,y,xlim=range(x),ylim=c(min(d[,2])-30,max(d[,2]+30)),main="Model based on original data")
      abline(m1$coef);legend("topleft",legend=bquote(italic(R)^2==.(r2_m1)),bty="n")
  }
  plot(x2,y2,xlim=range(x),ylim=c(min(d[,2])-30,max(d[,2]+30)),main="Model based on trimmed data")
  abline(m2$coef);
  
  legend("topleft",legend=bquote(atop(italic(R)^2==.(r2_m2),alpha==.(alpha))),bty="n")
  
  return(if(orig.ret==TRUE){list(m1=m1,m2=m2)} else{m2})
  }
  
  tlm2(d[,1],d[,2])
  
  tlm2(d[,1],d[,2],orig.plot=T)
  </code></pre>
  
  <p>Can anyone give me a hint?</p>
  
  <p>Thank You in advance!</p>