Sunday, August 05, 2007

Non-English Fonts in PostScript and PDF in R Graphics

Since R_2.3.0, R has released multilingual versions. If Users choose the option of "message translation" while installing R, they will get the R in chosen language. However, users need one more step to get their outputs that can show different language other than English correctly. To be sure, Paul Murrell and Brian Ripley has detailed documentation in R news about how to get this done!

Here is just a simple example of making a R graphic embedded with Chinese characters. For other language, please refer to the aforementioned documentation.

# R code
pdf("c:/Rtest.pdf", height=2, width=2, family="CNS1", version="1.3")
par(mar=c(1, 1 , 1, 1))
matplot(0:2, 0:2, type="n", axes=F, ylab="", xlab="")
box()
text(1, 1, "測試中文")
dev.off()





The crux is that you have do define a font family in your graphic command. For this example, the font family for Chinese is "CNS1." One caveat is that for some language, the version of PDF needed to be 1.3 or above, as shown in this example code.

No comments: