'Legend text cuts off using latex2exp in R plot
The text in my legend cuts off using library(latex2exp) in a legend.
library(stats)
library(evd)
library(dgumbel)
library(latex2exp)
x=seq(-5,10,length.out=1001)
fgumb=dgumbel(x,location=0,scale=1)
ffrech=dfrechet(x,loc=0,scale=1,shape=1)
fweib=dweibull(x,shape=2,scale=1)
maxes=max(fgumb,ffrech,fweib)
plot(x,fgumb,type='l',lwd=2,col='blue',ylim=c(0,maxes),
ylab="f(x)")
lines(x,ffrech,type='l',lwd=2,col='red')
lines(x,fweib,type='l',lwd=2,col='green')
legend("topright",
legend=c(TeX(r'(Gumbel(\mu=0,\sigma=1)'),
TeX(r'(Frechet(\mu=0,\sigma=1,\xi=1)'),
TeX(r'(Weibull(\sigma=1,\xi=2)')),
col=c("blue","red","green"),
lwd=c(2,2,2),cex=1)
Is there a way to correct this?
Solution 1:[1]
I'm not sure what the problem is, but I couldn't get your latex strings to parse at all. In any case, if you write them directly as R expressions, your problem resolves:
plot(x,fgumb, type = 'l' ,lwd = 2, col = 'blue', ylim = c(0, maxes), ylab = "f(x)")
lines(x, ffrech, type = 'l', lwd = 2, col = 'red')
lines(x, fweib, type = 'l', lwd = 2, col = 'green')
legend("topright",
legend = c(expression(paste('Gumbel(', mu==0~','~sigma==1, ')')),
expression(paste('Frechet(', mu==0~','~sigma==1, ',', xi==1, ')')),
expression(paste('Weibull(', sigma==1~','~xi==2, ')'))),
col = c("blue", "red", "green"),
lwd = c(2, 2, 2), cex = 1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Allan Cameron |


