Box and line in R plot Legend
Asked Answered
S

2

6

I've done a plot, and have one small problem which drive me crazy with the legend part

I can't find a way to center the box in the legend.

x = seq(-4,4,length=200)
y = dnorm(x,mean=0.816783035,sd=0.862916258)
plot(x,y,type="l",lwd=2,col="red" , xlim=c(-4, 4) ,  ylab="probabilitiy", xlab="")

 
# Shaded Area TBR!
c = -0.211653725

 
x = seq(-4,4,length=200)
y = dnorm(x,mean=-0.393475584,sd=0.895660247)
lines(x,y,type="l",lwd=2,col="blue")

 
legend("topright", legend = c('TBR', 'TBF', 'Criterion', 'Hit'), 
               bty = "n",
               col = c("green", "blue", "red",NA),
               lty = c(1,1,1,NA),
               density=c(0,0,0,10),
               fill = c("green", "blue", "red","red"),
               border = c(NA,NA,NA,"red"),
               )
abline(v= c)
 
x1 = seq(c,4, length = 200)
y1 = dnorm(x1,mean=0.816783035,sd=0.862916258)
 
polygon(c(c,x1) ,c(0, y1),density= 3, col="red")

enter image description here

Seductive answered 20/5, 2014 at 23:13 Comment(0)
E
7

It's the "x.intersp" parameter you will need to play with I think. Try:

x=seq(-4,4,length=200)
y=dnorm(x,mean=0.816783035,sd=0.862916258)
plot(x,y,type="l",lwd=2,col="red" , xlim=c(-4, 4) ,  ylab="probabilitiy", xlab="")


# Shaded Area TBR!
c = -0.211653725


x = seq(-4,4,length=200)
y = dnorm(x,mean=-0.393475584,sd=0.895660247)
lines(x,y,type="l",lwd=2,col="blue")


legend("topright", legend = c('TBR', 'TBF', 'Criterion', 'Hit'), 
               bty = "n",
               col = c("green", "blue", "red",NA),
               lty = c(1,1,1,NA),
               density=c(0,0,0,10),
               fill = c("green", "blue", "red","red"),
               border = c(NA,NA,NA,"red"), 
               x.intersp=c(2,2,2,0.5)
               )
abline(v= c)

x1 = seq(c,4, length = 200)
y1 = dnorm(x1,mean=0.816783035,sd=0.862916258)

polygon(c(c,x1) ,c(0, y1),density= 3, col="red")

enter image description here

Does that do what you want?

Eloign answered 20/5, 2014 at 23:20 Comment(2)
Looks right, though c(1,1,1,-0.5) might be a better fit as it doesn't seem to alter the spacing of the other parts of the legend.Mesic
plot() likely changed in the last ten years because x.intersp now sets the "character interspacing factor for horizontal (x) spacing between symbol and legend text." The above code will no longer work.Aixlachapelle
A
0

Method 1

Use merge=TRUE and they will overlap (horizontally). You can also shorten the lines a bit with seg.len if you wish.

I add the following to legend():

merge=TRUE, seg.len=1,

Full code:

x = seq(-4,4,length=200)
y = dnorm(x,mean=0.816783035,sd=0.862916258)
plot(x,y,type="l", lwd=2, col="red", xlim=c(-4, 4),  ylab="probabilitiy", xlab="")

# Shaded Area TBR!
c = -0.211653725

x = seq(-4,4,length=200)
y = dnorm(x,mean=-0.393475584,sd=0.895660247)
lines(x,y,type="l",lwd=2,col="blue")

 
legend("topright", legend = c('TBR', 'TBF', 'Criterion', 'Hit'), 
               bty = "n",
               col = c("green", "blue", "red",NA),
               lty = c(1,1,1,NA),
               density=c(0,0,0,10),
               fill = c("green", "blue", "red","red"),
               border = c(NA,NA,NA,"red"),
               merge=TRUE, seg.len=1,
               )
abline(v= c)
 
x1 = seq(c,4, length = 200)
y1 = dnorm(x1,mean=0.816783035,sd=0.862916258)
 
polygon(c(c,x1) ,c(0, y1),density= 3, col="red")

Method 2

Do it by setting the line segments lengths as negative. It draws the line right to left over the box symbol. You will also want to adjust the interspacing between symbols and text.

Adding the following to the legend:

seg.len=-1, x.intersp=2,

Full code:

x = seq(-4,4,length=200)
y = dnorm(x,mean=0.816783035,sd=0.862916258)

plot(x, y, type="l", lwd=2,col="red", xlim=c(-4, 4), ylab="probabilitiy", xlab="")

# Shaded Area TBR!
c = -0.211653725

x = seq(-4,4,length=200)
y = dnorm(x,mean=-0.393475584,sd=0.895660247)
lines(x,y,type="l",lwd=2,col="blue")

 
legend("topright", legend = c('TBR', 'TBF', 'Criterion', 'Hit'), 
               bty = "n",
               col = c("green", "blue", "red",NA),
               lty = c(1,1,1,NA),
               density=c(0,0,0,10),
               fill = c("green", "blue", "red","red"),
               border = c(NA,NA,NA,"red"),
               seg.len=-1, x.intersp=2,
               )
abline(v= c)
 
x1 = seq(c,4, length = 200)
y1 = dnorm(x1,mean=0.816783035,sd=0.862916258)
 
polygon(c(c,x1) ,c(0, y1),density= 3, col="red")

Difference / Conclusion

The difference between the two methods is the alignment of the box with the line edge, either the left or right.

You can have them all be the same with (or 'aligned') by reducing the width of the lines to 0.8:

seg.len=0.8
# or
seg.len=-08
Aixlachapelle answered 23/4 at 9:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.