[R] Legend in the Margins of a plot

partha_bagchi@hgsi.com partha_bagchi at hgsi.com
Fri May 14 18:47:18 CEST 1999


Jim,
Thanks for the code. That is what I meant when I talked about legends in
the margin. My situation was to put 4 plots on one page and then put the
legend at the bottom as they all shared the same legend. I finally used the
layout function with a fake 5th plot containing the legend.
PS: If anyone is using this code, please correct the minor typo on the 3rd
line of the function.



                                                                                                              
                    Jim Lemon                                                                                 
                    <bitwrit at ozemail.com        To:     r-help at stat.math.ethz.ch                              
                    .au>                        cc:                                                           
                    Sent by:                    Subject:     Re: [R] Legend in the Margins of a plot          
                    owner-r-help at stat.ma                                                                      
                    th.ethz.ch                                                                                
                                                                                                              
                                                                                                              
                    05/13/99 08:56 AM                                                                         
                                                                                                              
                                                                                                              



Partha,

If you mean the style of legend below or above the plot as often seen in
charting programs, this might be what you need.  You may have to set the
margins to get this to work - e.g.

>par(mar=c(7,4,4,2)) 
> plot(1:10,xlab="")
> legend.mar(legend=c("First","Second","Third"),lty=1:3,pch=1:3,col=1:3) 
The function is as follows:

> legend.mar
function (side = 1, xpos, ypos, legend, lty, pch, col)
{
    if (!missing(legend)) {
        oldpar <- par("adj","xpd)
        par(adj = 0, xpd = T)
        legendwidth <- strwidth(legend)
        markwidth <- strwidth("MM")
        totalwidth <- sum(legendwidth) + 3 * markwidth * length(legend)
        if (missing(xpos)) {
            xlim <- par("usr")[1:2]
            xcenter <- (xlim[1] + xlim[2])/2
            xpos <- xcenter - totalwidth/2
        }
        if (missing(ypos)) {
            ylim <- par("usr")[3:4]
            yoffset <- (ylim[2] - ylim[1])/6
            if (side != 1)
                ypos <- ylim[2] + yoffset
            else ypos <- ylim[1] - yoffset
        }
        if (missing(col))
            col <- rep(1, length(legend))
        for (i in 1:length(legend)) {
            if (!missing(pch))
                points(xpos, ypos, type = "p", pch = pch[i],
                  col = col[i])
            if (!missing(lty))
                lines(c(xpos - markwidth, xpos), c(ypos, ypos),
                  type = "l", lty = lty[i], col = col[i])
            xpos <- xpos + markwidth
            text(xpos, ypos, legend[i])
            xpos <- xpos + legendwidth[i] + 2 * markwidth
        }
        par(oldpar)
    }
    else cat("Usage: legend.mar([side=1] [, xpos] [, ypos], legend [,
lty] [, pch] [, col=1])\n")
}


Bit rough, but I hope it helps.

Jim


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._._



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list