Indian
Indian

Reputation: 997

R: Graph Plotting: Subscripts in the legend like LaTeX

I have the following code:

require(extrafont)

xaxis <- seq(0,1,by=0.01)

#set the functions here 
aij <- sqrt(1 - (1-xaxis)**1.02)
bij <- 1 - (1 - xaxis)**1.50
cij<-aij-bij

pdf("MyPlot.pdf",family="Times New Roman")
par(mfrow=c(1, 1), mar=c(5, 5, 1, 5))
plot(xaxis,aij,type="l", col="black",lty=1,ylim=c(0,1.3))
lines(xaxis,bij,type="l",col="black",lty=2)
par(new=TRUE)
plot(xaxis,cij,,type="l",col="black",lty=4,xaxt="n",yaxt="n",xlab="",ylab="")
grid(nx = 10, ny = 10)
xlab("Size ratio")
ylab("Values of the functions")

axis(4)
mtext("Difference", side=4,line=3)
legend(x='topright',col=c("black","black"),lty=c(1,2,3),legend=c("aij","bij","Difference"),bty="n")
dev.off()

I want the legend to appear as $a_{ij}$ and $b_{ij}$ as it does in LaTeX output. How do I do that?

Upvotes: 1

Views: 236

Answers (1)

Paulo MiraMor
Paulo MiraMor

Reputation: 1610

For subscripts in the legend, use the function expression, and the terms to be subscript between "[ ]" (square brackets):

legend(x='topright',col=c("black","black"),lty=c(1,2,3),legend=c(expression(a[ij]),expression(b[ij]),"Difference"),bty="n")

If you want it to be in italics, as it happens automatically in Latex math mode, use italic inside the square brackets:

legend(x='topright',col=c("black","black"),lty=c(1,2,3),legend=c(expression(a[italic(ij)]),expression(b[italic(ij)]),"Difference"),bty="n")

Upvotes: 3

Related Questions