I'm trying to control the position of a plot when converting to PDF using knitr and pandoc. My .Rmd file looks this:
# My report Some text some text some text some text some text some text some text some text some text ```{r myplot, echo=FALSE, fig.pos="placeHere", results='hide'} library(ggplot2) ggplot(mtcars, aes(mpg, drat)) + geom_point() ``` Some text some text some text some text some text some text some text some text some text \usepackage{graphicx} \begin{figure}[placeHere] \centering \includegraphics[width=0.5\textwidth]{placeHere} \end{figure} Some text some text some text some text some text some text some text some text some text
I'm converting to PDF using the functions provided here: http://quantifyingmemory.blogspot.co.uk/2013/02/reproducible-research-with-r-knitr.html
How can I place the plot between the second and third blocks of text? The latex code is not working as it currently stands.
EDIT: This is what I'm trying now.
# My report ```{r setup, include=FALSE} # set global chunk options opts_chunk$set(cache=FALSE) library(ggplot2) ``` ```{r, echo=FALSE, fig.height=3} ggplot(mtcars, aes(disp, hp)) + geom_point() ``` Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text ```{r, echo=FALSE, fig.height=3} ggplot(mtcars, aes(vs, am)) + geom_point() ``` Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text ```{r, echo=FALSE, fig.height=6} ggplot(mtcars, aes(disp, cyl)) + geom_point() ``` ```{r, echo=FALSE, fig.height=6} ggplot(mtcars, aes(hp, qsec)) + geom_point() ``` Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text ```{r, echo=FALSE, fig.height=3} ggplot(mtcars, aes(hp, wt)) + geom_point() ``` Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text ```{r, echo=FALSE, fig.height=5} ggplot(mtcars, aes(mpg, drat)) + geom_point() ``` Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text
Commonly used markup languages include Markdown, ReStructuredText, HTML, LaTex, ePub, and Microsoft Word DOCX. In plain English, Pandoc allows you to convert a bunch of files from one markup language into another one. Typical examples include converting a Markdown file into a presentation, LaTeX, PDF, or even ePub.
To center an image using the knitr::include_graphics() function, include it within an R code chunk that has the fig. align='center' option (and perhaps other options to control width, etc.).
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.
I present an alternative solution. Instead of inserting [H] symbols into a latex document in a post-hoc manner, I suggest redefining the figure environment to ignore any position arguments and use [H].
To do this, create a .tex file in the same directory as the .Rmd file which redefines the figure environment, and update the YAML header in the .Rmd to include the file during compilation.
Here is an example of a .tex file:
\usepackage{float} \let\origfigure\figure \let\endorigfigure\endfigure \renewenvironment{figure}[1][2] { \expandafter\origfigure\expandafter[H] } { \endorigfigure }
Here is example .Rmd which includes it (assuming you called the .tex file 'preamble-latex.tex'):
--- title: "example" author: "you" date: "`r format(Sys.time(), '%d %B %Y')`" output: rmarkdown::pdf_document: fig_caption: yes includes: in_header: preamble-latex.tex --- ```{r, fig.cap='Markdownvellous!'} plot(1:10, 1:10) ```
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