Dear R Users, If we issue simple plot command in R we don't get legend of the plot automatically. For example, following lines plots two curves, but to write a legend of these two curves there is no simple command. I checked with ?legend but it seems bit complicated for me. Does anyone know how to get a legend in a simple way for following R plot. Thanks, Yogesh>plot (x,y, type='n', ann=FALSE) >lines(x,y,col=1,lty="solid") >points(x,y,pch=16) > >points (x,y1, type='n', ann=FALSE) >lines(x,y1,col=1,lty="solid") >points(x,y1,pch=21)- [[alternative HTML version deleted]]
Hi, There is no way to obtain an automatical legend with plot. The choice you have is to draw the legend by yourself or to use another graphical package but it is not necessary easier... For example, the code for the package ggplot2: donnees <- data.fram(x=x,y=y,y1=y1) melt(donnees,id="x") -> donnees.m qplot(x,value,data=donnees.m,colour=variable,geom=c("point","line")) Alain On 22-Jul-10 12:17, Yogesh Tiwari wrote:> Dear R Users, > If we issue simple plot command in R we don't get legend of the plot > automatically. > > For example, following lines plots two curves, but to write a legend of > these two curves there is no simple command. I checked with ?legend but > it seems bit complicated for me. Does anyone know how to get a legend in a > simple way for following R plot. > Thanks, Yogesh > > > >> plot (x,y, type='n', ann=FALSE) >> lines(x,y,col=1,lty="solid") >> points(x,y,pch=16) >> >> points (x,y1, type='n', ann=FALSE) >> lines(x,y1,col=1,lty="solid") >> points(x,y1,pch=21) > > > - > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Alain Guillet Statistician and Computer Scientist SMCS - IMMAQ - Universit? catholique de Louvain Bureau c.316 Voie du Roman Pays, 20 B-1348 Louvain-la-Neuve Belgium tel: +32 10 47 30 50
There is no simpler way. It's not automatic but it is fairly simple at the most basic. It can get a bit complicated later. ltxt <- c("cat","dog") x <- 1:5 y <- 1:5 plot (x,y, type='n', ann=FALSE) lines(x,y,col=1,lty="solid") points(x,y,pch=16) legend(1,4,ltxt, pch=16) --- On Thu, 7/22/10, Yogesh Tiwari <yogesh.mpi at googlemail.com> wrote:> From: Yogesh Tiwari <yogesh.mpi at googlemail.com> > Subject: [R] how to write legend of a plot > To: "r-help" <r-help at stat.math.ethz.ch> > Received: Thursday, July 22, 2010, 6:17 AM > Dear R Users, > If we issue simple plot command in R we don't get legend of > the plot > automatically. > > For example, following lines plots two curves, but to write > a legend of > these two curves there is no simple command. I checked with > ?legend? but > it seems bit complicated for me. Does anyone know how to > get a legend in a > simple way for following R plot. > Thanks, Yogesh > > > > >plot (x,y, type='n', ann=FALSE) > >lines(x,y,col=1,lty="solid") > >points(x,y,pch=16) > > > >points (x,y1, type='n', ann=FALSE) > >lines(x,y1,col=1,lty="solid") > >points(x,y1,pch=21) > > > > - > > ??? [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org > mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, > reproducible code. >