Home > database >  Legend with couple of values
Legend with couple of values

Time:10-03

I need to a legend like the following one at the topright of the plot (code below):

(media, sigma)
value1, value1
value2, value2
value3, value3
...

This is the code I wrote to try to get the right legend, but the result is not good:

passo = 0.01
MEDIA = c(1/4, 1/2, 1, 2, 4)
SIGMA = c(5, 4 ,3, 2, 1)
len = length(MEDIA)
x = seq(0, 10, passo)

plot(x=NULL, y=NULL, xlim=c(0, 10), ylim=c(0, 0.6), xlab='x', ylab='densità' )
title("densità gaussiana")

for (i in 1:len){
    media = MEDIA[i]
    sigma = SIGMA[i]
    lines(x, dnorm(x, media, sigma), type='l', col=i)
}

legend( 'topright',  legend = MEDIA, col = 1 : len,  lty = 1, cex = 0.4)
legend( 'topright',  legend = SIGMA, col = 1 : len,  lty = 1, cex = 0.4)

CodePudding user response:

You can combine the MEDIA and SIGMA vectors to get the output you want. With the title option you can put the description above. Like so:

legend( 'topright', title = "(media, sigma)",
    legend = paste0("(",MEDIA, ", ", SIGMA, ")"),
    col = 1 : len,  lty = 1, cex = 0.4)
  • Related