Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unicode character with subscript

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.

like image 997
Nadja Simons Avatar asked Mar 19 '15 16:03

Nadja Simons


People also ask

How do you add a subscript to a character?

To make text appear slightly above (superscript) or below (subscript) your regular text, you can use keyboard shortcuts. Select the character that you want to format. For superscript, press Ctrl, Shift, and the Plus sign (+) at the same time. For subscript, press Ctrl and the Equal sign (=) at the same time.

What is the symbol for subscript?

Use "_" (underscore) for subscripts.


1 Answers

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]))
like image 193
konvas Avatar answered Sep 28 '22 14:09

konvas