6

I have problem with special characters in my knitr document. Normal text works fine but the polish letters in the title or axis name on the picture doesn't. enter image description here

I tried to fix this with installing cairoDevice package. But it still doesn't work. Actually I'm not sure if this package works properly, because I had to install also Rgtk2 package and there were problems with it too. Is there any other way to havepolish letters, another than cairoDevice? If not how to make it work properly?

Here's my knitr file problem.Rnw:

\documentclass[12 pt, a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{polski}
\usepackage{geometry}
\geometry{total={210mm, 297mm}, left=17mm, right=17mm, top=17mm, bottom=17mm}
\usepackage[font=footnotesize,labelfont=bf]{caption}
\usepackage{url}
\usepackage[colorlinks=true]{hyperref}
\hypersetup{linkcolor=blue}

<<include=F>>=
source("My path/plot.R")
@

<<include=F>>=
library(knitr)
library(RGtk2)
library(Cairo)
library(cairoDevice)

#ustawienia globalne
opts_chunk$set(fig.path='figure/', fig.align='center', fig.show='asis', fig.pos='ht!', dev='CairoPDF')

@

\begin{document}

<<MusicBad, echo=F, fig.width=5, fig.height=5, out.width='.5\\linewidth', fig.cap='Błędne zastosowanie wykresu liniowego.'>>=

plot.music.bad

@
\end{document}

and here my plot.R file:

library(ggplot2)

#create data frame
music <- c("Blues", "Hip-hop", "Jazz", "Metal", "Rock")
number <- c(8, 7, 4, 6, 11)
df.music <- data.frame(music, number)

#create line graph 
plot.music.bad <- ggplot(data=df.music, aes(x=music, y=number, group=1)) +
  geom_line(size=1, color="#333333") +
  geom_point(size=3, color="#333333") +
  xlab("Rodzaj muzyki") +
  ylab("Liczba studentów") +
  ylim(c(0,11)) +
  ggtitle("Ulubiony typ muzyki wśród studentów")

Do you know how to fiz this? I just want to have polish letters in the graphics. Thank you.

EDIT My sessioninfo:

sessionInfo()

R version 2.15.3 (2013-03-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=Polish_Poland.1250  LC_CTYPE=Polish_Poland.1250   
[3] LC_MONETARY=Polish_Poland.1250 LC_NUMERIC=C                  
[5] LC_TIME=Polish_Poland.1250    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] tools_2.15.3

If I run this plot from console it works fine. I had the same problem with saving it to pdf but if I run cairo_pdf(name, width=width, height=height) instead of pdf(name, width=width, height=height) it it saved with special characters.

jjankowiak
  • 3,010
  • 6
  • 28
  • 45
  • 1
    might help to state your `sessionInfo()` (esp. locale and OS). Do you get good results if you just plot from the console? – Ben Bolker May 14 '14 at 23:12
  • Search for `Encoding` on this page: http://yihui.name/knitr/demo/graphics/ – Yihui Xie May 16 '14 at 06:05
  • Also, try calling `Sys.setlocale('LC_CTYPE', 'Polish_Poland.UTF-8')` in the first chunk (I'm not sure if it works on Windows, though) – gagolews May 22 '14 at 09:25

1 Answers1

1

You should put this in the beginning to change encoding:

<<result='hide'>>=
Sys.setlocale('LC_CTYPE', 'pl_PL.UTF-8')
@
Jot eN
  • 6,120
  • 4
  • 40
  • 59