statistics and applied probability

national university of singapore

Chinese text in R graphs

Here’s the problem I was having: I use the English-language localisation of R, but for a couple of plots needed to include some Chinese text as well.  I didn’t want to have to mess around with image editors, but wanted to output the graph as a pdf.

A call for help from my Sinophonic colleagues led to David C sending this:

library(grid)
pdf(“chinese.pdf”, width = 10, height = 7)
plot(1:10)
grid.text(“華語”, y=7/8, gp=gpar(fontfamily=”CNS1″))
grid.text( “is ‘chinese’ in (Traditional) Chinese”, y=6/8)
grid.text(“华语”, y=5/8, gp=gpar(fontfamily=”GB1″))
grid.text(“is ‘chinese’ in (Simplified) Chinese”, y=4/8)
dev.off()
Seems you can either put the characters directly in or use unicode (e.g. the similar example from Murrel & Ripley, 2006, Non-Standard Fonts in PostScript and PDF Graphics.  R News 6:41–6):
grid.text(“\u4F60\u597D”, y=2/3, gp=gpar(fontfamily=”CNS1″))
Note that if the reader doesn’t have this font installed on his/her computer, they’ll not see aught.  Seems this should be addressable by embedding the font in the file, but I can’t work out how to do this as something isn’t installed that should be on my computer.  :o(  Anyway, the graph comes out nice on my computer at least.

3 Comments

  1. Alex

    2010/03/31 at 14:40

    PS You might have more luck embedding than me. Murrel & Ripley suggest the embedFonts() command to post-process the file.

  2. I tried your code on my linux box – the grid.text command produces faulty PDF code and both Acrobat Reader and gv complain without showing anything…argh

  3. Alex

    2010/04/01 at 11:11

    I get it to work on xpdf and acroread, but not in kpdf or okular. Do you have the Chinese text packs installed?

Leave a Reply

Your email address will not be published.

*

Skip to toolbar