7

I want to add a Unicode character which has two letters as subscripts to my plot legend in R. The character is an r with an accent breve (ř) and the two letters are i and j.

I already looked at this question: Unicode character with superscript and tried to adapt the answers to my problem.

Here is what I tried:

plot(1,pch=NA,ylab="",xlab="",axes=F)
legend("top",legend=paste("1-","\u{0159}"),bty ="n",bg = "white",cex=2)
legend("center",legend=paste("1-","\u{0159}","\u{0069}","\u{006A}"),bty="n",bg = "white",cex=2)
legend("bottomleft",legend=expression("1-"*"\u0159"["\u0069"*"\u006A"]),bty="n",bg = "white",cex=2)
legend("bottomright", legend = quote("1-" *"\u0159"["\u0069"*"\u006A"]),bty="n",bg = "white",cex=2)

The resulting plot can be found below

enter image description here

Both the Unicode letter and the subscript work fine by themselves but not together. paste() with any combination of [ ] does return an error, but I think this is to be expected as paste can't handle [ ] for subscripts.

The FAQ site on CRAN might give a hint as I am using Windows but I am not sure how to implement this:

3.6 I don't see characters with accents at the R console, for example in ?text.

You need to specify a font in Rconsole (see Q5.2) that supports the encoding in use. This used to be a problem in earlier versions of Windows, but now it is hard to find a font which does not.

Support for these characters within Rterm depends on the environment (the terminal window and shell, including locale and codepage settings) within which it is run as well as the font used by the terminal window. Those are usually on legacy DOS settings and need to altered.

Nadja Simons
  • 1,196
  • 1
  • 7
  • 10
  • 1
    using the answer you linked to, this works for me `plot.new(); text(0.5, 0.7, labels = quote("1 - " * '\u{0159}'[ij]))`. 3 and 4 in your example also work for me – rawr Mar 19 '15 at 17:06
  • @rawr Unfortunately, your solution did not work for me. I am using RStudio so I also tried it using plain R, but also there none of the versions worked. – Nadja Simons Mar 19 '15 at 19:13
  • Nadja, what OS are you using, as the last two of your examples and rawr's code, both render as expected on ubuntu 14.04, Rv3.1.3 – user20650 Mar 19 '15 at 20:37
  • @user20650 I am using Windows 7 – Nadja Simons Mar 20 '15 at 07:54
  • Hi @konvas; thanks for the suggestion but the same problem. I wonder if it is a locale or encoding problem, but if that is the case, it is strange that the accented letter displays as expected without the subscript. `legend=quote("\u0159")` displayes the accented letter, but adding the subscript `legend=quote("\u0159"[i]`), removes it. As with the OP, this doesnt work on W7, R3.1.1. – user20650 Mar 23 '15 at 14:39
  • What R version are you using (post the result of sessioninfo() in R) – cmbarbu Mar 23 '15 at 15:52

1 Answers1

4

It has to do with the system locale, as seen e.g. if you try

# intToUtf8(345)
# [1] "ř"
# iconv(intToUtf8(345), "utf-8", localeToCharset())
# [1] "r"

This should fix it (I used Czech but other locales will probably work too):

Sys.setlocale("LC_CTYPE", "czech")
# [1] "Czech_Czech Republic.1250"
text(..., labels = quote("\u{0159}"[ij]))
konvas
  • 14,126
  • 2
  • 40
  • 46
  • Great stuff, cheers. I had tried setting locale to German, and French with no joy. ..czech is the answer! – user20650 Mar 23 '15 at 16:17
  • @user20650 Lol yes it's the first language that came to mind that has this character, although I'm sure there will be many. Do you have any idea though why it works without changing the locale when there are no subscripts involved, eg in `quote("\u{0159}")` ? – konvas Mar 23 '15 at 16:29
  • 1
    Not a clue, i also find that strange. So perhaps the real answer is *don't use Windows* – user20650 Mar 23 '15 at 16:40