15

I'm using expression() in R plots in order to get italicized text. But it appears as if I cannot use Unicode symbols inside expression outside of ASCII characters. Is there some way I can work around this? My goal is to get the fi ligature in various labels in my R barplots (together with italicized text).

I'm using R for Windows version 3.0.2.

CairoPDF(file = "Ligature1.pdf")
plot.new()
text(x =.5, y = .5, labels = "fi", family = "Times New Roman")
dev.off()

enter image description here

CairoPDF(file = "Ligature2.pdf")
plot.new()
text(x =.5, y = .5, labels = expression(paste(italic(m), "u", "fi", italic(m), sep = "")), family = "Times New Roman")
dev.off()

enter image description here

zero323
  • 322,348
  • 103
  • 959
  • 935
Sverre
  • 747
  • 2
  • 8
  • 19
  • Works fine on Debian / R 3.0.2 - [`sessionInfo()`](http://pastebin.com/p25qReW7) – zero323 Nov 09 '13 at 19:34
  • Not able to reproduce on a Mac, either. The `paste` looks unnecessary. What do you see with `italic(m)*u*fi*italic(m)`? – IRTFM Nov 10 '13 at 18:28
  • @DWin: Exactly the same as in my question. – Sverre Nov 10 '13 at 18:36
  • My answer to your other question should be helpful then. – IRTFM Nov 10 '13 at 18:39
  • 3
    Seems like the answer here as well is that R for Windows is having issues with UTF-8. – Sverre Nov 10 '13 at 18:53
  • 1
    I can reproduce the problem on Windows. You can simplify the example down to this (using the ordinary windows() device instead of pdf): `plot.new() ; text(x =.5, y = .5, labels = expression(paste(italic(m), "u", "fi", italic(m), sep = "")))` – Kevin Wright Sep 12 '14 at 20:32
  • So this issue still isn't fixed on Windows, apparently :( – GFauxPas Dec 21 '16 at 21:48
  • This seems to be similar to your question: http://stackoverflow.com/questions/23324872/rstudio-not-picking-the-encoding-im-telling-it-to-use-when-reading-a-file I am curious is there is a solution there for you. – Bewc Dec 29 '16 at 16:15
  • The OP asked for a work-around. It is not too hard to produce the output that he requested under Windows by printing the `italic` part and the Unicode part separately. I am not posting as an answer because while it answers the original question, I do not think it addresses the problem that the bounty was intended for. – G5W Dec 30 '16 at 15:34
  • @G5W it would also be nice to have italicized unicode. – GFauxPas Dec 30 '16 at 20:42
  • 1
    Yes, all I seem to be able to get on my Windows system is a mixture of formatted ASCII and unformatted Unicode. No formatted Unicode. – G5W Dec 30 '16 at 20:52

1 Answers1

5

You asked for a work-around. That is all this is. The italic part needs expression, but the "fi" part does not, so print them separately.

plot.new()
offset1 = strwidth(expression(paste(italic(m), "u")), units="figure")
text(x =.5, y = .5, labels = expression(paste(italic(m), "u", sep="")))
text(x =.5+offset1, y = .5, labels ="fi")
offset2 = strwidth("fi ")
text(x =.5+offset1+offset2, y = .5, labels = expression(italic(m)))

formatted output

But notice that something is not quite right about the spacing of the "fi", so when I computed the width on the screen, I cheated and computed the width of "fi " (with an extra blank). Without the extra spacing, the second italic(m) overlapped the "fi".

Not pretty, but it produces the desired result under Windows.

G5W
  • 36,531
  • 10
  • 47
  • 80