It is not as easy as it may seem. Technically, this problem is related to OS / locale / pdf writer / Sweave (see "Installing and Administering R", Chapter 7). Since I am running GNU / Linux, this “solution” is not addressed to Mac and Windows users. And to make things a little more complicated, GNU / Linux distributions are different from each other, so there is a good chance that if something works, say, Ubuntu does not work on Arch Linux.
I will use the mtcars . Let me create a base graph with localized characters:
pdf("foo.pdf") boxplot(mpg ~ cyl, data = mtcars, ylab = "Potrošnja goriva", xlab = "Broj cilindara", main = "Dijagram raspršenja") dev.off()
(Serbian crash course: "Potrošnja goriva" means fuel consumption, "Broj cilindara" means the number of cylinders and "Dijagram raspršenja" is equivalent for dispersion)
Now I get a bunch of warnings:
Warning messages: 1: In title(ylab = "Potrošnja goriva", xlab = "Broj cilindara", main = "Dijagram raspršenja") : conversion failure on 'Dijagram raspršenja' in 'mbcsToSbcs': dot substituted for <c5> 2: In title(ylab = "Potrošnja goriva", xlab = "Broj cilindara", main = "Dijagram raspršenja") : conversion failure on 'Dijagram raspršenja' in 'mbcsToSbcs': dot substituted for <a1> 3: In title(ylab = "Potrošnja goriva", xlab = "Broj cilindara", main = "Dijagram raspršenja") : conversion failure on 'Dijagram raspršenja' in 'mbcsToSbcs': dot substituted for <c5> 4: In title(ylab = "Potrošnja goriva", xlab = "Broj cilindara", main = "Dijagram raspršenja") : conversion failure on 'Dijagram raspršenja' in 'mbcsToSbcs': dot substituted for <a1> 5: In title(ylab = "Potrošnja goriva", xlab = "Broj cilindara", main = "Dijagram raspršenja") : conversion failure on 'Potrošnja goriva' in 'mbcsToSbcs': dot substituted for <c5> 6: In title(ylab = "Potrošnja goriva", xlab = "Broj cilindara", main = "Dijagram raspršenja") : conversion failure on 'Potrošnja goriva' in 'mbcsToSbcs': dot substituted for <a1> 7: In title(ylab = "Potrošnja goriva", xlab = "Broj cilindara", main = "Dijagram raspršenja") : conversion failure on 'Potrošnja goriva' in 'mbcsToSbcs': dot substituted for <c5> 8: In title(ylab = "Potrošnja goriva", xlab = "Broj cilindara", main = "Dijagram raspršenja") : conversion failure on 'Potrošnja goriva' in 'mbcsToSbcs': dot substituted for <a1>
While options(encoding = "CP1250") does not do the trick - I get the same warnings, pdf.options(encoding = "CP1250") fixes it, and the same means pdf(file = "foo.pdf", encoding = "CP1250") . So, I will return my old encoding using options(encoding = "native.enc") , set pdf.options , as mentioned earlier, and get everything right.
Some users leave by installing pdf.options and not getting problems with Sweave. So, you have to paste this part of the code somewhere in the .Rnw file before you start printing:
<<setOptions, echo = FALSE, results = hide>>== pdf.options(encoding = "CP1250") @
and later, just do:
<<plotTheFigure, echo = TRUE, fig = TRUE>>== # I've set echo to TRUE intentionally, to prove my point here boxplot(mpg ~ cyl, data = mtcars, ylab = "Potrošnja goriva", xlab = "Broj cilindara", main = "Dijagram raspršenja") @
And the same script means ggplot2 graphics.
Some of you will get the right result, but I won’t! And, as I said, if you use Ubuntu, there is a big chance that this will work, but so far I can not make it live and kick in Arch.
And to save your keystrokes, you can download the Sweave file and / or PDF file (executed on the Arch machine). As you can see, localized characters are displayed correctly in the graph function, but are distorted in Sweave. Now, if I try to save the graph to a PDF file (without Sweaving), I get the correct output .
So, I solved some problems, but many trial errors and errors remained.
Run the .Rnw file on your computer and give me some feedback. To make things easier, I created Rscript that collects your system information (and not personal information), which I consider relevant in this case: here is source , and here is my output .