0

I want to write this below text(LaTeX Code) in legend of the plot But I tried, I couldn't succeed. Any suggestion? ‎R_2(‎\delta_{JS}(\bX,W),\theta)-R_2(\bX,W) ‎R_2(‎\delta_{JS}(\bX,W),\theta)\R_2(\bX,W)

Thank you in advance.

user2227801
  • 85
  • 1
  • 8
  • Did you search for this? Check http://stackoverflow.com/questions/1395105/getting-latex-into-r-plots – Ferdinand.kraft Mar 30 '13 at 19:26
  • 1
    It's rather annoying to have questions closed (especially when done by people with limited experience with R) when the supposed duplicate is an out-of-date answer. The tikzDevice has been removed from CRAN so the accepted answer is simply wrong and the citations in Harrell's page cited in his answer are now 404'ed. So maybe the answer is ... use plotmath ... but I was sort of hoping my answer which I considered not really that great could get improved by someone who either reads Latex or by the OP or by someone who can direct us to a replacement for the tikzDevice. – IRTFM Mar 31 '13 at 01:43

2 Answers2

1

The reason I did not agree with the vote to close is that now that the tikzDevice is missing in action, I do not know of an automatic way to do the translation to plotmath, so what follows in an inefficient hack that might provoke a much better answer if someone has a better way to generate Latex in R plots.

You should be able to use a plotmath equivalent in the labels argument to text(). My trouble was that I don't read LaTeX or TeX so I used: http://www.codecogs.com/latex/eqneditor.php to build something I could read. If there are other resources that I could have used I would be happy to hear of them. (My efforts to locate such resources took a fair amount of time and I kept wondering if I could have done better.)

enter image description here

So in R that is fairly complicated but I think doable:

?plotmath
plot(1,1)
text(1.1, 1.2, labels=expression(R[2]*( delta[JS]*(phantom(0)*","*W)*","* theta)- 
                                  R[2]*(phantom(0)*","*W)*R[2]*( delta[JS]*(phantom(0)*
                                   ","*W)*","* theta)[2](phantom(0)*","*W)) )

enter image description here If you want italic:

text(1.1, 0.8, labels=expression( italic( R[2]*( delta[JS]*(phantom(0)*","*W)*","* theta)- 
                                  R[2]*(phantom(0)*","*W)*R[2]*( delta[JS]*(phantom(0)*
                                   ","*W)*","* theta)[2](phantom(0)*","*W)) ) )

And if you wanted serif fonts add this to the text arguments ,family="serif"

enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487
0

An example of adding expression to xlab of a plot (which is similar to adding LaTeX code I guess):

library(ggplot2)
data(cars)
qplot(speed, dist, data=cars) + xlab(expression(alpha + frac(beta, gamma)))
dirkchen
  • 195
  • 7