Hi folks, Do you mind if I ask a related question that I have been having trouble with - how do you put the legend outside of the plot area (to the bottom of the area - below the x-axis title)? Could anybody show me using the example given below:
x <- seq(-pi, pi, len = 65) plot(x, sin(x), type="l", col = 2) legend(x = -3, y = .9, "legend text", pch = 1, xjust = 0.5) Thank you, I've not been able to do this simple bit of programming and it is very frustrating not to be able to add a simple key. Best Wishes, Jenny Hi Emili, Even though you are calling your horizontal coordinate y, and vertical coordinate z, the first and second arguments to legend(), namely x and y, should be the horizontal and vertical coordinates, respectively; and they are given in user coordinates (e.g., legend()'s x should be between 1960 and 1975 and legend()'s y should be between 1 and 4). If you want to use normalized coordinates (i.e. 0 to 1), you can scale as in this example: legend(x = par("usr")[1] + diff(par("usr")[1:2])*normalizedCoordX, y = par("usr")[3] + diff(par("usr")[3:4])*normalizedCoordY, ...) where normalizedCoordX and Y go from 0 to 1 (see ?par, par("usr") returns vector of c(xmin,xmax,ymin,ymax) of user coordinates on a plot) You can alternatively use legend(x = "topleft",...) or "bottomright", and so on to place your legend. If you want to add your legend outside of the plot, you should consider increasing the margins using the 'mar' argument in par(), and also setting par(xpd=TRUE) (so stuff can show up outside of the plotting region). Best regards, ST > y<-c(1960, 1965, 1970, 1975) > z<-c(1, 2, 3, 4) within the data limits of your x and y) --- Emili Tortosa-Ausina <[EMAIL PROTECTED]> wrote: > Hi to all, > > I'm sorry for posting this question, I am sure I am missing something > important but after reading the documentation I cannot find where the > problem is. > > I want to add a legend to a figure. If I use a simple example drawn > from the R Reference Manual such as, for instance: > > x <- seq(-pi, pi, len = 65) > plot(x, sin(x), type="l", col = 2) > legend(x = -3, y = .9, "legend text", pch = 1, xjust = 0.5) > > then everything works just fine. > > However, if I use other data such as, for instance: > > y<-c(1960, 1965, 1970, 1975) > z<-c(1, 2, 3, 4) > plot(y, z, type="l", col = 2) > legend(x = -3, y = .9, "legend text", pch = 1, xjust = 0.5) > > then the legend is not shown. > > Any hints? > > Thanks in advance, > > Emili > > ______________________________________________ > R-help@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 > and provide commented, minimal, self-contained, reproducible code. > ________________________________________________________________________________ ____ Food fight? Enjoy some healthy debate ______________________________________________ R-help@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 and provide commented, minimal, self-contained, reproducible code. ------------- End Forwarded Message ------------- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Jennifer Barnes PhD student: long range drought prediction Climate Extremes Group Department of Space and Climate Physics University College London Holmbury St Mary Dorking, Surrey, RH5 6NT Tel: 01483 204149 Mob: 07916 139187 Web: http://climate.mssl.ucl.ac.uk ______________________________________________ R-help@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 and provide commented, minimal, self-contained, reproducible code.