Is there a simpler way then the solution to the one that was posted here? I'm not very proficient with legend, and I don't understand this solution. All I have is two or more lines on one plot that I want to put a legend on and I can't figure out how to do it from the examples. Can you give a very simple example? It does not have to be fancy!! I have never worked with a package where the legend was not automatic. ---------- Forwarded Message ---------- Subject: Re: [R] legend() with option adj=1 Date: Wed, 21 May 2003 09:19:11 +0200 From: Uwe Ligges <ligges at statistik.uni-dortmund.de> To: Jerome Asselin <jerome at hivnet.ubc.ca> Cc: r-help at stat.math.ethz.ch Jerome Asselin wrote:> Hi there, > > I want to justify to right the text of my legend. Consider this short > reproducable example. > > x <- 1:5 > y1 <- 1/x > y2 <- 2/x > plot(rep(x,2),c(y1,y2),type="n",xlab="x",ylab="y") > lines(x,y1) > lines(x,y2,lty=2) > legend(5,2,c("1,000","1,000,000"),lty=1:2,xjust=1,yjust=1) > legend(5,1.5,c("1,000","1,000,000"),lty=1:2,xjust=1,yjust=1,adj=1) > > Now, I would like to right-justify the text of the legend. As you can see, > the option adj=1 does not give satisfactory results. > > Is this a bug or is there an easy way that I'm missing? > > Thanks, > JeromeWorks, e.g., with the following little trick: x <- 1:5 y1 <- 1/x y2 <- 2/x plot(rep(x,2),c(y1,y2),type="n",xlab="x",ylab="y") lines(x,y1) lines(x,y2,lty=2) temp <- legend(5, 2, legend = c(" ", " "), text.width = strwidth("1,000,000"), lty = 1:2, xjust = 1, yjust = 1) text(temp$rect$left + temp$rect$w, temp$text$y, c("1,000", "1,000,000"), pos=2) See ?legend for details, in particular the returned value. Uwe Ligges ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Anna H. Pryor wrote:> Is there a simpler way then the solution to the one that was posted here? I'm > not very proficient with legend, and I don't understand this solution. All > I have is two or more lines on one plot that I want to put a legend on and I > can't figure out how to do it from the examples. Can you give a very simple > example? It does not have to be fancy!! I have never worked with a package > where the legend was not automatic.Hmm. The simple solution is to use legend() as is, see ?legend for details. The solution given below was intended for the specific question on right justified legend text. Uwe Ligges> > > ---------- Forwarded Message ---------- > > Subject: Re: [R] legend() with option adj=1 > Date: Wed, 21 May 2003 09:19:11 +0200 > From: Uwe Ligges <ligges at statistik.uni-dortmund.de> > To: Jerome Asselin <jerome at hivnet.ubc.ca> > Cc: r-help at stat.math.ethz.ch > > Jerome Asselin wrote: > >>Hi there, >> >>I want to justify to right the text of my legend. Consider this short >>reproducable example. >> >>x <- 1:5 >>y1 <- 1/x >>y2 <- 2/x >>plot(rep(x,2),c(y1,y2),type="n",xlab="x",ylab="y") >>lines(x,y1) >>lines(x,y2,lty=2) >>legend(5,2,c("1,000","1,000,000"),lty=1:2,xjust=1,yjust=1) >>legend(5,1.5,c("1,000","1,000,000"),lty=1:2,xjust=1,yjust=1,adj=1) >> >>Now, I would like to right-justify the text of the legend. As you can see, >>the option adj=1 does not give satisfactory results. >> >>Is this a bug or is there an easy way that I'm missing? >> >>Thanks, >>Jerome > > > Works, e.g., with the following little trick: > > x <- 1:5 > y1 <- 1/x > y2 <- 2/x > plot(rep(x,2),c(y1,y2),type="n",xlab="x",ylab="y") > lines(x,y1) > lines(x,y2,lty=2) > temp <- legend(5, 2, legend = c(" ", " "), > text.width = strwidth("1,000,000"), lty = 1:2, xjust = 1, yjust = 1) > text(temp$rect$left + temp$rect$w, temp$text$y, > c("1,000", "1,000,000"), pos=2) > > See ?legend for details, in particular the returned value. > > Uwe Ligges > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
On Thu, 5 Jun 2003 07:20:11 -0700 "Anna H. Pryor" <anna at ptolemy.arc.nasa.gov> wrote:> Is there a simpler way then the solution to the one that was posted here? I'm > not very proficient with legend, and I don't understand this solution. All > I have is two or more lines on one plot that I want to put a legend on and I > can't figure out how to do it from the examples. Can you give a very simple > example? It does not have to be fancy!! I have never worked with a package > where the legend was not automatic. > > > > ---------- Forwarded Message ---------- > > Subject: Re: [R] legend() with option adj=1 > Date: Wed, 21 May 2003 09:19:11 +0200 > From: Uwe Ligges <ligges at statistik.uni-dortmund.de> > To: Jerome Asselin <jerome at hivnet.ubc.ca> > Cc: r-help at stat.math.ethz.ch > > Jerome Asselin wrote: > > Hi there, > > > > I want to justify to right the text of my legend. Consider this short > > reproducable example. > > > > x <- 1:5 > > y1 <- 1/x > > y2 <- 2/x > > plot(rep(x,2),c(y1,y2),type="n",xlab="x",ylab="y") > > lines(x,y1) > > lines(x,y2,lty=2) > > legend(5,2,c("1,000","1,000,000"),lty=1:2,xjust=1,yjust=1) > > legend(5,1.5,c("1,000","1,000,000"),lty=1:2,xjust=1,yjust=1,adj=1) > >. . .> > Uwe LiggesHere is an alternative to consider. In plots such as this I like to label the curves where they are most separated and avoid legends altogether (as well as usually avoiding the need for different line types, unless curves intertwine): x <- 1:5 y <- 1/x y2 <- 2/x w <- list('1/x'=list(x=x,y=y),'2/x'=list(x=x,y=y2)) library(Hmisc) # see http://hesweb1.med.virginia.edu/biostat/s/Hmisc.html labcurve(w, pl=TRUE, offset=.1) Or put a legend in the most empty region of the graph: labcurve(w, pl=TRUE, lty=c(2,1), lwd=c(1,3), col=gray(c(0,.7)), keys='lines', xlab=expression(chi)) # lty only for demonstration - omit that for this example. Thick gray scale # lines are excellent for step functions Or use same line types but put symbols every so often (point.inc= to override default spacing; this works well for overlapping step functions also): labcurve(w, pl=TRUE, keys=1:2) # uses pch=1:2 --- Frank E Harrell Jr Prof. of Biostatistics & Statistics Div. of Biostatistics & Epidem. Dept. of Health Evaluation Sciences U. Virginia School of Medicine http://hesweb1.med.virginia.edu/biostat