I am trying to troubleshoot a noticeable difference in font size when comparing plots generated for a knitr
document. HTML/markdown output appears to be what I would consider the correct size while the PDF/Latex output is approximately 2-4 pts larger (e.g., if I was expecting 12pt, the output is closer 14 or 16 pts). Note the issue seems to be limited to the plot/figure text and doesn't seem to manifest with other text in the output document.
The simplest way for me to demonstrate this is with the default content that RStudio (version 0.99.329) populates when a new *.Rmd
file is created. A side-by-side comparison is available via this link. To my eye, the HTML version is properly sized and the PDF/Latex version is not.
To my knowledge, I don't have any custom settings within my .Rprofile or other locations that could be impacting this issue. I have tried disabling/enabling pdf crop and that does not seem to impact the issue either. I have duplicated the issue on a colleagues machine (also running OS X and the same version of RStudio).
Outside of the rmarkdown
framework (i.e., just running R code) there does not appear to be such a difference between PDF and PNG output. For example, the following code produces output that is fairly similar:
library(ggplot2)
r <- ggplot(data = diamonds,
aes(x = carat, y = price, color = cut, group = cut))
r + geom_smooth(size = 2) +
ggtitle("Plant growth with\ndifferent treatments")
ggsave(file="test.pdf")
ggsave(file="test.png")
Given that this code above produces output plots that are fairly similar, I'm suspicious the issues I'm seeing are related to knitr or the rmarkdown/pandoc conversion process.
So, my main question is whether the level of differences I'm seeing between output formats is expected? Am I the only one? Is it unique to my system?
If it is expected behavior, how are folks reconciling the issue? For ggplots, I've been using theme_bw(8.5)
to downscale the fonts for PDF/latex output. This works, but it adds another level of complexity when creating output for multiple platforms/uses from the same *.Rmd
file ... one of the key benefits of rmarkdown
.
Specifications of my setup and system
Changing the font sizes in markdown can be done by employing HTML tags and specifying the number for the font. Both the entire text and a specific section of it can have their font size altered.
The first code chunk: ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` is used to specify any global settings to be applied to the R Markdown script. The example sets all code chunks as “echo=TRUE”, meaning they will be included in the final rendered version.
knitr is an engine for dynamic report generation with R. It is a package in the programming language R that enables integration of R code into LaTeX, LyX, HTML, Markdown, AsciiDoc, and reStructuredText documents. The purpose of knitr is to allow reproducible research in R through the means of literate programming.
When you press “Knit to PDF” in RStudio, it converts your R Markdown document into LaTeX. Download MiKTeX from here: https://miktex.org/download • Run the installer, and restart your computer. Open an R Markdown file in RStudio, and try knitting to PDF. You may be prompted to install some packages.
Have you tried this solution, which sets different output devices for different output formats? Here it is:
Solution to producing pdf and html output from a unique Markdown file by setting specific options to knitr in the Makefile:
$(PDF): $(SRC) Makefile
Rscript \
-e "library(knitr)" \
-e "opts_chunk[['set']](dev = 'pdf')" \
-e "pat_gfm()" \
-e "knit('$<', 'temp.md')"
$(PANDOC) temp.md -o $@
rm temp.md
This answer also has interesting ideas.
Here it is:
Try putting this code chunk at the beginning of the Rmd document.
```{r setup, cache=FALSE, include=FALSE}
library(knitr)
output <- opts_knit$get("rmarkdown.pandoc.to")
if (output=="html") opts_chunk$set(fig.width=11, fig.height=11)
if (output=="docx") opts_chunk$set(fig.width=6, fig.height=6)
```
One of the package options returned by opts_knit$get() is markdown.pandoc.to. This is evidently set to "html", "docx", or "latex" depending on the chosen output format (HTML, Word, or PDF). So you can test that and set the chunk options fig.width and fig.height accordingly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With