Reputation: 155
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")
Upvotes: 6
Views: 5862
Reputation: 3597
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")
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")
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
Upvotes: 0
Reputation: 1233
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")
Does that do what you want?
Upvotes: 7