Special characters in knitr (cairoDevice doesn't work)
Asked Answered
M

1

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.

Midshipman answered 14/5, 2014 at 22:58 Comment(3)
might help to state your sessionInfo() (esp. locale and OS). Do you get good results if you just plot from the console?Catania
Search for Encoding on this page: yihui.name/knitr/demo/graphicsNickelson
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)Hildegardhildegarde
B
1

You should put this in the beginning to change encoding:

<<result='hide'>>=
Sys.setlocale('LC_CTYPE', 'pl_PL.UTF-8')
@
Bumble answered 20/7, 2014 at 20:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.