Align symbols in the legend

2019-08-13 07:22发布

问题:

I woult like to insert a legent to my plot. I tried this with the following code:

plot(1:4)
legend("topleft",legend=expression(paste(theta[1]==7%*%10^-4,sep="  ",theta[2]==-14%*%10^-4),
                                  paste(theta[1]==0,sep="  ",theta[2]==0)),bty="n",cex=1.2)

Unfortunately, as you can see, the symbols of the legend are not not aligned in the vertical sence. So theta_2 in the second line appears under the value of theta_1 of the first line.

How can I change the code, so that the symbols are well aligned?

回答1:

There is an ncol argument....

plot(1:4)
legend("topleft",legend= expression( paste(theta[1]==7%*%10^-4) ,
  paste(theta[1]==0)  ,
  paste( theta[2]==-14%*%10^-4),
  paste( theta[2]==0 ) ),
  bty="n",cex=1.2 , ncol = 2 )



回答2:

phantom() can help, it replaces its argument by white space of specific width,

plot(1:4)
legend("topleft",legend=expression(theta[1]==7%*%10^-4~~theta[2]==-14%*%10^-4,
                                   theta[1]==0*phantom(""%*%10^-4)~~theta[2]==0),
       bty="n",cex=1.2)