Reputation: 911
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. 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!
Upvotes: 0
Views: 1676
Reputation: 10196
I think it looks better without a box ( legend( ... ,bty='n')
) but if you really want a box, here's how you do it:
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)
Upvotes: 1