Unicode in R graphics

The first solution

Simona and Kristijan have (September 2012) a problem with writing characters čšž in R graphics.

After some searching on the web (see urls) I found the following solution

Sys.setlocale(category="LC_CTYPE", locale="slovenian")
setwd("D:/work/R/test")
x <- 1:10; y <- x**2
pdf.options(encoding = "CP1250")
pdf(file="csz0.pdf")
plot(x,y,xlab="\U0161irina",ylab="višina")
text(1,95,"\u010C \u0160\u017D")
text(1,90,"\u010D \u0161\u017E")
text(1,85,"Č ŠŽ")
text(1,80,"č šž")
dev.off()

The problem with this solution is that the characters Č and č are overlapping with the other text. The “solution” is to insert an additional space.

csz0.pdf

Unicode and SVG

The right solution would be the use of Unicode, but it seems that in R in PDF and Postscript it is not fully supported. There is a way around - R supports also the vector graphics in SVG.

setwd("C:/Users/Batagelj/test/R/svg")
x <- 1:10; y <- x**2
svg("csz.svg")
plot(x,y,xlab="\U0161irina",ylab="višina")
text(1,95,"\u010C\u0160\u017D",pos=4)
text(1,90,"\u010D\u0161\u017E",pos=4)
text(1,85,"ČŠŽ",pos=4)
text(1,80,"čšž",pos=4)
text(1,75,"\u0409\u0443\u0431\u0459\u0430\u043D\u0430",pos=4)
text(1,70,"\u00A9\u03B1\u222B\u263A\u20AC\u2640\u2642",pos=4)
dev.off()

It works!

csz.svg

To get the picture in PDF we open it in Inkscape and save it in PDF.

csz.pdf

There is also a direct way to use Unicode in PDF based on the library Cairo. You can check whether Cairo is supported in your version of R using the command capabilities() .

> capabilities()
    jpeg      png     tiff    tcltk      X11     aqua http/ftp  sockets   libxml 
    TRUE     TRUE     TRUE     TRUE    FALSE    FALSE     TRUE     TRUE     TRUE 
    fifo   cledit    iconv      NLS  profmem    cairo 
   FALSE     TRUE     TRUE     TRUE     TRUE     TRUE 

The R on Windows supports it. So I tried:

library(cairoDevice)
Cairo_pdf("csz1.pdf",width=7,height=7,pointsize=10)
plot(x,y,xlab="\U0161irina",ylab="višina")
text(1,95,"\u010C\u0160\u017D",pos=4)
text(1,90,"\u010D\u0161\u017E",pos=4)
text(1,85,"ČŠŽ",pos=4)
text(1,80,"čšž",pos=4)
text(1,75,"\u0409\u0443\u0431\u0459\u0430\u043D\u0430",pos=4)
text(1,70,"\u00A9\u03B1\u222B\u263A\u20AC\u2640\u2642",pos=4)
dev.off()

We get

csz1.pdf

It (almost) works! It seems that in the Unicode font used by Cairo some characters do not have a description.

If we replace the command Cairo_pdf with

> Cairo(width=7,height=7,pointsize=10)

and delay the command dev.off() we can get the picture also on the screen.

URLs

NEUREJENO

notes/fontr.txt · Last modified: 2015/07/16 20:43 by vlado
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki