Hi:

Try this, using the parText function given below:

# Generate the x's, vectors of rate and shape, densities and matplot
x <- 5 * ppoints(100)
rate <- rep(c(2, 4), each = 3)
shape <- rep(c(1, 3, 5), 2)
gamden <- matrix(NA, nrow = 6, ncol = 100)
for(i in 1:6) gamden[i, ] <- dgamma(x, rate[i], shape[i])
matplot(x, t(gamden), type = 'l', lty = 1:6, col = 1:6, ylab = 'density')

For the legend, we have this little function that takes the parameter
(vector) as the first argument and its value (vector) as the second:

parText <- function(names, pars) {
     p1 <- paste("* '= ", pars[-length(pars)], ", '*", sep = "")
     p2 <- paste("* '= ", pars[length(pars)], "'")
     p <- c(p1, p2)
     txt <- paste(names, p, collapse = "")
     parse(text = txt)
    }

We need to create a vector of names and a matrix of values to
call the function:

names <- c('lambda', 'theta')
vals <- cbind(rate, shape)

Now use it to create the rows of the legend:

legnames <- c(parText(names, vals[1, ]), parText(names, vals[2, ]),
               parText(names, vals[3, ]), parText(names, vals[4, ]),
               parText(names, vals[5, ]), parText(names, vals[6, ]))

Finally, generate the legend:

legend(locator(1), legend = legnames, lty = 1:6, col = 1:6)

Hope this is what you wanted...
Dennis

On Wed, Feb 3, 2010 at 3:54 PM, casperyc <caspe...@hotmail.co.uk> wrote:

>
> i=1
> for(rate in c(2,4) ){
>        for(shape in c(1,3,5) ){
>        curve(dgamma(x,rate,shape),xlim=c(0,3),ylab="",col=i,lty=i,add=T)
>        i=i+1
>        }
> }
>
> How can I add some legend to represent these lines?
> i.e. the legend is displayed as
>
> col=1 lty=1 lambda=2 theta=1
> col=2 lty=2 lambda=2 theta=3
> col=3 lty=3 lambda=2 theta=5
> col=4 lty=4 lambda=4 theta=1
> col=5 lty=5 lambda=4 theta=3
> col=6 lty=6 lambda=4 theta=5
>
> i tried to use
> legend( locator(1), expression(lambda=i theta=i),col=i,lty=i)
> but did actually get it
>
> can someone help?
>
> Thanks!
>
> casper
> --
> View this message in context:
> http://n4.nabble.com/legend-help-tp1461992p1461992.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@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.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to