Non English characters in ggplot within a knitr document - r

I'm trying to knit to pdf a file with Lithuanian characters like ąčęėįšųž in RStudio from .Rmd file. While knitting to html works properly and the ggplot title has the Lithuanian characters, when knitting to pdf ggplot does create warnings and dismisses these characters.
Reproducible example:
---
title: "Untitled"
output:
pdf_document:
includes:
in_header: header_lt_text.txt
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
```
## Lithuanian char: ĄČĘĖĮŠŲŪžąčęėįšųūž
```{r}
ggplot(iris, aes(Sepal.Length, Sepal.Width))+
geom_point(aes(col=Species))+
labs(title="Lithuanian char: ĄČĘĖĮŠŲŪžąčęėįšųūž")
```
I pass the header_lt_text.txt with follwoing arguments:
\usepackage[utf8]{inputenc}
\usepackage[L7x]{fontenc}
\usepackage[lithuanian]{babel}
\usepackage{setspace}
\onehalfspacing
Any suggestions on how to make ggplot create correct labels?

The problem is with the pdf device and is only apparent when saving the picture as pdf (which you want because it looks much, much better). This is why it seems to "work" in some cases: the image is not rendered as pdf but e.g. as png. Thanks to #Konrad for correctly identifying the source of the problem.
To solve this, you need to pass the correct encoding to the pdf device.
Fortunately, the pdf device (?pdf) takes an encoding argument and there is a chunk option to pass arguments to the device: dev.args
On Windows, an appropriate encoding is CP1257.enc (Baltic):
```{r dev="pdf", dev.args=list(encoding="CP1257.enc")}
ggplot(iris, aes(Sepal.Length, Sepal.Width))+
geom_point(aes(col=Species))+
labs(title="Lithuanian char: ĄČĘĖĮŠŲŪžąčęėįšųūž")
```
You can see the other encodings available out of the box with: list.files(system.file("enc", package = "grDevices"))
Works well on my linux machine:
Alternatively, if you're happy to get png images inserted in the pdf, you can simply use dev="png" in your chunk option. Doesn't look as good though.

Related

Is there a way to add line breaks ONLY when exporting to PDF in R Markdown?

I think the question is quite self-explanatory but for avoidance of doubt I'll explain with more detail below:
I have an R Markdown document that works well if converted to HTML or uploaded to GitHub. When converting to PDF (using Latex), the results are not so pretty. I find that the biggest problem in a Latex PDF document are line breaks. I can fix the line breaks issue on the PDF document by adding "\ " characters, but that throws my HTML document out of whack too.
Is there a way to manually add line breaks (or "space before/after paragraphs") for the PDF output only?
Thank you!
You can redefine the relevant spacings in the YAML header. \parskip controls the paragraph spacing. Code blocks are shaded using a snugshade environment from the framed package. We can also redefine the shaded environment for code blocks to have some vertical space at the start. Here's a reproducible example. Note: I also added the keep_tex parameter so you can see exactly what the generated tex file looks like, in case this is useful:
title: "test"
author: "A.N. Other"
header-includes:
- \setlength{\parskip}{\baselineskip}
- \renewenvironment{Shaded}{\vspace{\parskip}\begin{snugshade}}{\end{snugshade}}
output:
pdf_document:
keep_tex: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Once you output to HTML, you can just print the HTML webpage as PDF. that might be an easy way keep the original format

Plots generated in chunk don't appear with rmarkdown::render_site()

I am creating a website with
Rscript -e "rmarkdown::render_site()"
I am generating both html and pdf versions of documents. A plot generated in a chunk does not appear unless the pdf doc is generated before the html doc.
Here are the files:
index.Rmd
---
title: "My Website"
---
* [Test1 page](test1.html)
* [Test2 page](test2.html)
_site.yml
name: "my-website"
test1.Rmd (html generated first)
---
output:
html_document: default
pdf_document: default
---
```{r, message=FALSE, echo=FALSE}
library(ggplot2)
ggplot(mtcars, aes(x=mpg, y=disp)) + geom_point()
```
test2.Rmd (pdf generated first)
---
output:
pdf_document: default
html_document: default
---
```{r, message=FALSE, echo=FALSE}
library(ggplot2)
ggplot(mtcars, aes(x=mpg, y=disp)) + geom_point()
```
After running render_site() via Rscript, test1.html is blank --- there is no test1_files subdirectory. However, test2.html shows this plot (and of course test2_files exists):
This happens with both Rmarkdown 1.10 and 1.10.14, the development version as of October 31.
In a more complicated real life example, the plots don't appear even if I switch the document order, but I am hoping that the answer to this problem will help with the more complicated one.
UPDATE: In addition to the suggestions by #giocomai, a workaround is to compile test1.Rmd twice:
Rscript -e "rmarkdown::render_site()"
Rscript -e "rmarkdown::render_site('test1.Rmd')"
This seems to work even if you compile multiple single files. Presumably the clean-up is less aggressive in the single-file case.
I could replicate your problem, and I think this is related to the fact that rmarkdown::render() cleans the files after it creates a pdf output, as it thinks those files are useless, and render_site copies the files to the _site folder only after all output types have been rendered.
In rmarkdown::render() there is an option to set clean=FALSE, but it does not seem to be available to rmarkdown::render_site(), as arguments are not passed to render.
I think it would be worth filing it as an issue to Rmarkdown, as it shouldn't be too difficult to pass over the argument.
As a workaround, you can force cache = TRUE in the chunks of the relevant Rmd document. So, for, example, the code chunk in your test1.Rmd would look like:
```{r, message=FALSE, echo=FALSE, cache = TRUE}
library(ggplot2)
ggplot(mtcars, aes(x=mpg, y=disp)) + geom_point()
```
notice the cache = TRUE in the chunk options. With the cache enabled, the folder is preserved and it is correctly copied to the _site folder.
You can also set knitr::opts_chunk$set(cache = TRUE) for all chunks.
This solves your problem, but there should probably be more elegant solutions.

Plotly as png in knitr/rmarkdown

The following Rmarkdown renders the plotly 3D graph in HTML, but not in PDF.
Testing plotly
```{r}
library(plotly)
p <- plot_ly(data=iris, x=~Sepal.Length, y=~Sepal.Width, z=~Petal.Length,
color=~Species, symbols=c(0,1), type="scatter3d", mode="markers")
p
```
A snapshot of the graph appears as follows:
According to the plotly help page:
If you are using rmarkdown with HTML output, printing a plotly object in a code chunk will result in an interactive HTML graph. When using rmarkdown with non-HTML output, printing a plotly object will result in a png screenshot of the graph.
Is there a way to render the plotly graph in a PDF?
Note: The error from rmarkdown::render() is:
Error: Functions that produce HTML output found in document targeting latex output.
Please change the output type of this document to HTML. Alternatively, you can allow
HTML output in non-HTML formats by adding this option to the YAML front-matter of
your rmarkdown file:
always_allow_html: yes
Note however that the HTML output will not be visible in non-HTML formats.
I have created a little workaround, which saves the plotly images locally as png-file and imports it back to the RMD file.
You need the package webshot, which you can load via:
install.packages("webshot")
Further more, you need to install phantomjs via
webshot::install_phantomjs()
Then (when phantomjs is in your PATH), you can create your RMD file:
---
title: "Untitled"
output: pdf_document
---
```{r}
library(plotly)
p <- plot_ly(economics, x = ~date, y = ~unemploy / pop)
tmpFile <- tempfile(fileext = ".png")
export(p, file = tmpFile)
```
![Caption for the picture.](`r tmpFile`)
This works for me .. perhaps it's a workaround for you, too!
As #hrbrmstr commented, export() previously didn't support WebGL at all, but more recent versions support exporting to png via RSelenium (see help(export, package = "plotly")). If you need pdf export, you'll have to pay for a cloud account -- https://plot.ly/products/cloud/
Same issue with R markdown compile error:. You need to choose what format you want to KNIT to, tried to look at mine.
---
title: "<img src='www/binary-logo.jpg' width='240'>"
subtitle: "[<span style='color:blue'>binary.com</span>](https://github.com/englianhu/binary.com-interview-question) Interview Question I"
author: "[<span style='color:blue'>®γσ, Lian Hu</span>](https://englianhu.github.io/) <img src='www/ENG.jpg' width='24'> <img src='www/RYO.jpg' width='24'>白戸則道®"
date: "`r Sys.Date()`"
output:
tufte::tufte_html:
toc: yes
tufte::tufte_handout:
citation_package: natbib
latex_engine: xelatex
tufte::tufte_book:
citation_package: natbib
latex_engine: xelatex
link-citations: yes
---
What I do so that rendering to PDF's work but you can still have the interactive plots in other knit types and in rmarkdown files in r studio is:
this goes in the setup block (or really, anywhere early in the file):
is_pdf <- try (("pdf_document" %in% rmarkdown::all_output_formats(knitr::current_input())), silent=TRUE)
is_pdf <- (is_pdf == TRUE)
then this is used to render any plotly plot based on what kind of document you are creating:
if (is_pdf) { export(base_plot) } else {base_plot}
in the example base_plot is the name of the plot.

Changing page size within Rmarkdown document

I have a very large phylogenetic tree that I'd quite like to insert into a supplementary material I'm writing using Rmarkdown and knitr. I dislike splitting trees across pages and I doubt anybody would print this out anyway so I thought I'd just have a large page in the middle of the pdf I'm generating.
The question is how do I change page size for one page in an otherwise A4 document? I'm pretty new to knitr and I've found global paper size options but I'm struggling to find ways of setting up what would be the equivalent of sections in Word.
(Update) Hi does anybody else have a suggestion? I tried the pdfpages package but this seems to result in an equally small figure on a page the size of the pdf that is being pasted in i.e. if I make a 20in by 20in pdf figure then paste the page in using \includepdf then I get a 20in by 20in page with a much smaller figure on it (the same as the \eject example above). It seems like knitr or Latex is forcing the graphics to have a specific size regardless of page size. Any ideas? Here's a reproducible example:
---
title: "Test"
output:
pdf_document:
latex_engine: xelatex
header-includes:
- \usepackage{pdfpages}
---
```{r setup, include=FALSE}
require(knitr)
knitr::opts_chunk$set(echo = FALSE,
warning=FALSE,
message=FALSE,
dev = 'pdf',
fig.align='center')
```
```{r mtcars, echo=FALSE}
library(ggplot2)
pdf("myplot.pdf", width=20, height=20)
ggplot(mtcars, aes(mpg, wt)) + geom_point()
dev.off()
```
#Here's some text on a normal page, the following page is bigger but has a tiny figure.
\newpage
\includepdf[fitpaper=true]{myplot.pdf}
You should be able to use \ejectpage like this:
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
\eject \pdfpagewidth=20in \pdfpageheight=20in
```{r mtcars}
library(ggplot2)
ggplot(mtcars, aes(mpg, wt)) + geom_point()
```
\eject \pdfpagewidth=210mm \pdfpageheight=297mm
Back
(I can only remember the A4 height in mm for some reason)
I have just faced the same struggle when dealing with a large phylogenetic tree.
Based on hrbrmstr's answer, I came up with the snippet below.
The trick is to make knitr generate the figure but not include it in the intermediate .tex right away (hence fig.show="hide"). Then, because figure paths are predictable, you can insert it with some latex after the code chunk.
However, the new page height is not taken into account when positioning page numbers, so they tend to be printed over your image. I have tried to overcome this behavior multiple times, but in the end I simply turned them off with \thispagestyle{empty}.
---
output: pdf_document
---
```{r fig_name, fig.show="hide", fig.height=30, fig.width=7}
plot(1)
```
\clearpage
\thispagestyle{empty}
\pdfpageheight=33in
\begin{figure}[p]
\caption{This is your caption.}\label{fig:fig_name}
{\centering \includegraphics[height=30in, keepaspectratio]{main_files/figure-latex/fig_name-1} }
\end{figure}
\clearpage
\pdfpageheight=11in

How to use otf-font in ggplot2 graphics?

I want to use an otf-font in my ggplot2 graphics. I'm using Mac OS X 10.9. I found http://goo.gl/dFqJhV
But the experimental part won't work for me.
http://goo.gl/rNmIRR doesn't work either because I got lot's of errors compiling the branch. The mentioned branch seems to be too old.
The solution with cairo_pdf() (http://goo.gl/LcYFS6) does work but not for me, because I want to embed the graphics into a knitr file with output pdf and/or html.
You can try the showtext package combined with knitr using the fig.showtext=TRUE option.
Assume that the OTF font has a filename "somefont.otf", and you want to use it in R through the family name "myfont" (can be an arbitrary character string). Below is an example of an Rmd file that can be compiled into PDF or HTML:
---
output: pdf_document
---
```{r setup, include=FALSE}
library(showtext)
font.add("myfont", "somefont.otf")
```
```{r fig.showtext=TRUE, fig.width=7, fig.height=7}
plot(cars, family = "myfont")
```

Resources