Discussion:
annotate parse fails in ggplot2
Juan Carlos Suárez
2018-09-22 21:00:00 UTC
Permalink
Hi there

I have a bunch of R scripts doing plots with ggplot2 (nice package!). Since
I plot plenty of greek variables, I decided to use latex2exp package to
display greek character in ggplot2 plots.
This worked for me without any problem, but now it does not longer work. I
have been investigating if the problem may come from updates in the
packages, but
I am not able to find where is the problem.

The point is that I use greek chacarcters in both the x and y labels of the
plot, and inside de plot with "annotate_text".
This is an example of code:

r <- 1
label_R <- TeX(sprintf('$R = %i\\,\\mu Hz$', r), output = "character")

a <- ggplot(df, aes(x = myseq, y = mim)) + geom_line() +
annotate("text", x = 60 + 15, y = 0.04,
label = label_R, parse = T)
a

When I run the code, I get the following error messages
label_R <- TeX(sprintf('$R = %i\\,\\mu Hz$', r), output = "character")
Error in TeX(sprintf("$R = %i\\,\\mu Hz$", r), output = "character") :
unused argument (output = "character")
a <- ggplot(df, aes(x = myseq, y = mim)) + geom_line() +
+ annotate("text", x = 60 + 15, y = 0.04,
+ label = label_R, parse = T)
a
Error in parse(text = as.character(lab)) : <text>:1:1: unexpected '$'
1: $
^

I checked that "output = character" is not an option in TeX, however, if I
use no option within TeX
the object created has attribute "character" for output. In any case, all
the three options
"expression", "text", and "ast" yield the same error message.

If I turn parse option in annotate to "F", it displays the expression
without being parsed and no error.
So I think the problem comes from the parsing procedure, which might have
changed at some
of the latest ggplot updates or R updates.

Any idea? thanks in advance!
JC

PS: I run the code with the latest version of RStudio and R:

R version 3.5.1 (2018-07-02) -- "Feather Spray"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin15.6.0 (64-bit)
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Marios Barlas
2018-09-23 08:29:38 UTC
Permalink
Hi Juan,

If you only want to plot Greek letters or equations into ggplot labels or annotations I strongly suggest you use the expression() statement.

There are numerous examples online on how to use the syntax.

I suspect that the package you are using is not maintained for the latest versions of R and ggplot and I don't see the reason to downgrade really.
Here's sth to get you started.

https://stackoverflow.com/questions/5293715/how-to-use-greek-symbols-in-ggplot2

Then remember ^is superscript [] is subscript and you replace the regular space with the ~ in the syntax.

Just a few random tips. If you have a specific problem on syntax don't hesitate to ask!

Good luck end enjoy ggplot!
Mario

Get Outlook for Android<https://aka.ms/ghei36>

________________________________
From: ***@googlegroups.com <***@googlegroups.com> on behalf of Juan Carlos Suárez <***@gmail.com>
Sent: Saturday, September 22, 2018 11:00:00 PM
To: ggplot2
Subject: annotate parse fails in ggplot2

Hi there

I have a bunch of R scripts doing plots with ggplot2 (nice package!). Since I plot plenty of greek variables, I decided to use latex2exp package to
display greek character in ggplot2 plots.
This worked for me without any problem, but now it does not longer work. I have been investigating if the problem may come from updates in the packages, but
I am not able to find where is the problem.

The point is that I use greek chacarcters in both the x and y labels of the plot, and inside de plot with "annotate_text".
This is an example of code:

r <- 1
label_R <- TeX(sprintf('$R = %i\\,\\mu Hz$', r), output = "character")

a <- ggplot(df, aes(x = myseq, y = mim)) + geom_line() +
annotate("text", x = 60 + 15, y = 0.04,
label = label_R, parse = T)
a

When I run the code, I get the following error messages
Post by Juan Carlos Suárez
label_R <- TeX(sprintf('$R = %i\\,\\mu Hz$', r), output = "character")
Error in TeX(sprintf("$R = %i\\,\\mu Hz$", r), output = "character") :
unused argument (output = "character")
Post by Juan Carlos Suárez
a <- ggplot(df, aes(x = myseq, y = mim)) + geom_line() +
+ annotate("text", x = 60 + 15, y = 0.04,
+ label = label_R, parse = T)
Post by Juan Carlos Suárez
a
Error in parse(text = as.character(lab)) : <text>:1:1: unexpected '$'
1: $
^

I checked that "output = character" is not an option in TeX, however, if I use no option within TeX
the object created has attribute "character" for output. In any case, all the three options
"expression", "text", and "ast" yield the same error message.

If I turn parse option in annotate to "F", it displays the expression without being parsed and no error.
So I think the problem comes from the parsing procedure, which might have changed at some
of the latest ggplot updates or R updates.

Any idea? thanks in advance!
JC

PS: I run the code with the latest version of RStudio and R:

R version 3.5.1 (2018-07-02) -- "Feather Spray"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin15.6.0 (64-bit)

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com<mailto:ggplot2+***@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...