1

How to manually add legend latexed labels in qplot(), using TeX(r'()')? I have tried the following but it does not produce the desired result.

require(reshape2, ggplot2, latex2exp)
mat = matrix(1, nrow = 5, ncol = 2)
colnames(mat) = c("a","b")
df <- data.frame(m = 1:5, mat)
df.melted <- melt(df, id="m")

q <- qplot(x=m, y=value, color=variable, data=df.melted, geom="line")
q + scale_fill_discrete(name="Title",
                    breaks=c("a", "b"),
                    labels=c(TeX(r'($\tau$)'),TeX(r'($\tau$)')))

Note, expression() works, but I want TeX(r'()') since the latter is more convenient to use.

Aditya Ghosh
  • 111
  • 4
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Sep 07 '21 at 04:12
  • Where do the values `mlist` and `est.mat` come from? Please make sure your example is [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so we can copy/paste the code to run and test it. What is the version using `expression()` that does what you want? – MrFlick Sep 07 '21 at 07:17
  • Also i'm not sure visual differences you are expecting here. The `TeX` function just turns the string into an R `?plotmath` expression. In this case you are just using "a" and "b" which won't look any different when plotted as an expression. Are you trying to make them appear italic? The `TeX()` function has a separate `italic=` parameter for that. – MrFlick Sep 07 '21 at 07:29
  • I used a and b just as an example. In general I might need something complicated, which is way easier to write using Tex(r'()') than expression(). – Aditya Ghosh Sep 08 '21 at 09:39

0 Answers0