简体   繁体   English

R图中图例中的标题位置

[英]title position in legend in R plot

I use the following code to draw a plot. 我使用以下代码绘制图。

x = seq(-20,20,by=0.2);
c = .2;
y1 = exp(c*x);
c = .5;
y2 = exp(c*x);
c = 1;
y3 = exp(c*x);
par(mgp = c(2,.5,0));   # to adjust dist of x/y label to plot, x/y axes to plot
plot(x,
     y1,
     type="l",
     xlab=expression(x-theta),
     ylab=expression(L(x,theta)),
     main="function");
lines(x,y2,col="blue");
lines(x,y3,col="green");
legend(x=-20,
       y=40,
       title=expression(L(x, theta)==e^{c(x-theta)}),
       legend=expression("c=.2", "c=.5", "c=1"),
       lty=c(1, 1, 1),
       lwd=c(2.5, 2.5,2.5),
       col=c("black","blue","green"));

I find that the brackets of $(x-\\theta)$ in the legend exceed the box. 我发现图例中$(x- \\ theta)$的括号超出了框。 Is there any way to move that expression down? 有什么办法可以降低该表达? I tried replacing the original legend function by 我尝试通过替换原始图例功能

legend(x=-20,y=40,title.adj=c(0,.5), title=expression(L(x,theta)==e^{c(x-theta)}), legend=expression("c=.2","c=.5","c=1"), lty=c(1,1,1), lwd=c(2.5,2.5,2.5), col=c("black","blue","green"));

However, the expression appeared twice in the legend box. 但是,该表达式在图例框中出现了两次。

Thanks! 谢谢!

I think it looks better without a box ( legend( ... ,bty='n') ) but if you really want a box, here's how you do it: 我认为没有框会更好看( legend( ... ,bty='n') ),但是如果您真的想要一个框,可以按照以下步骤操作:

lgnd = legend(x=-20,
               y=40,
               title=expression(L(x, theta)==e^{c(x-theta)}),
               legend=expression("c=.2", "c=.5", "c=1"),
               lty=c(1, 1, 1),
               lwd=c(2.5, 2.5,2.5),
               col=c("black","blue","green"),
               # no box
               bty='n');

# plot your own box using the lgnd$rect as your starting point
params = lgnd$rect
rect(xleft = params[['left']],
    ybottom = params[['top']] - params[['h']],
    xright = params[['left']] + params[['w']],
    ytop = params[['top']] + 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM