R图中的方框和线条图例

Boo*_*Boo 5 plot r

我已经完成了一个情节,但有一个小问题使我对图例部分感到疯狂

我找不到在图例中将框居中的方法。

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")
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Tha*_*Guy 6

我认为这是您需要使用的“x.intersp”参数。尝试:

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")
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

这样做你想要的吗?