Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in using TeX inside geom_text() #13

Open
caijun opened this issue May 4, 2018 · 3 comments
Open

Error in using TeX inside geom_text() #13

caijun opened this issue May 4, 2018 · 3 comments

Comments

@caijun
Copy link

caijun commented May 4, 2018

I would like to annotate math formulas on the plot using geom_text() and I specified the TeX expression to the label argument in following code.

library(ggplot2)
library(latex2exp)

p <- ggplot(mtcars, aes(mpg, cyl)) + 
  geom_point() + 
  geom_text(aes(x = 30, y = 7, label = TeX("$\\theta$ = 270")))
print(p)

However, it produces following error

Don't know how to automatically pick scale for object of type expression. Defaulting to continuous.
Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class ‘"expression"’ to a data.frame
@caijun
Copy link
Author

caijun commented May 5, 2018

This can be resolved by specifying the output of TeX() as character, although "character" is not one of the values that the output argument can take, and turning the parse argument in geom_text() to TRUE. Like following code,

library(ggplot2)
library(latex2exp)

p <- ggplot(mtcars, aes(mpg, cyl)) + 
  geom_point() + 
  # labs(title = TeX("$\\theta$ = 270"))
  geom_text(aes(x = 30, y = 7, label = TeX("$\\theta$ = 270", output = "character")), 
            parse = TRUE)
print(p)

@dalewsteele
Copy link

This appears to be a new issue (for me) related to some change in ggplot2. The workaround above worked for me as documented. See stackoverflow question below. Thanks.

https://stackoverflow.com/questions/50957441/annotate-a-plot-made-with-ggplot2-with-an-equation-using-latex2exptex

@stefano-meschiari
Copy link
Owner

#12 is fixed as of 0.9.0. I will add a note about ggplot in the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants