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
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