Plot Margins in Rmarkdown/Knitr

Adjusting figure margins in Rmarkdown

You might try using the chunk option 'out.width'. Here is the Rmd file that I used. I think that it does what you want.

    ---
output: pdf_document
---

```{r, out.width='\\textwidth', fig.height = 8, fig.align='center'}
pie(c(0.57, 0.43), font = 2, col = c("tomato", "white"))

pie(c(0.57, 0.43), font = 2, col = c("blue", "orange"))
```

Plot margins in RMarkdown/knitr

For people that want directly the answer hidden within the comment and a tweet, your .Rmd file should look like:

---
title: exemple
header of your Rmd
---

```{r}
library(knitr)
opts_knit$set(global.par = TRUE)
```
The important think in the previous chunk is to put global.par=TRUE

Plot one depends on values set by `par()`:

```{r}
par(mar=c(5,5,0,0)) #it's important to have that in a separate chunk
```
Now we just plot a point with the good margin set by the global `par()`

```{r}
plot(1,1,main="a plot with the margin as set globally")
```

if you don't want to include the code used to set the global margin in the output just add :

```{r,include=FALSE}
par(mar=c(5,5,0,0))
```

in the chunk you don't want to include.

How to control the margins of figures when knitting a HTML in R?

You might try the fig.height and fig.width chunk options. Not sure, but I think these might work because they adjust the graphics device as discussed here

Reduce title margins when creating PDF from Rmarkdown using knitr

Using LaTeX code, you can:

  • reduce the space between the title and the text with \vspace{-1cm}

  • reduce the top margin with the same code placed in title in the YAML

Here's your example:

---
title: \vspace{-1.5cm} test
output:
pdf_document:
latex_engine: xelatex
geometry: margin = 0.1in
---

\vspace{-1cm}
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

a bunch of text to demonstrate that the margin applies only to the body text of
this document, while the title (above) remains quite far from the top of the
document, and this is messing up my plan to make a document that's only one page in length

Margin too wide in PDF output of rmarkdown

Let's try to solve your problem with this simple example, which I made for you.

---
title: "test"

header-includes:
\usepackage{geometry}
\geometry{top=0.75in,left=3in,bottom=0.75in,right=0.80in}

output:
pdf_document
---

Is it work at your machine - yes or no? \
By me - it works! \
^_^ \

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 pressure, echo=FALSE}
plot(pressure)
```

Sample Image

Does it work right on your PC?

Rmarkdown to pdf: change plot margins without showing the code resposible for changing the margins

From knitr docs.

besides TRUE/FALSE which completely turns on/off the source code, we
can also use a numeric vector to select which R expression(s) to echo
in a chunk, e.g. echo=2:3 means only echo the 2nd and 3rd expressions,
and echo=-4 means to exclude the 4th expression

R Markdown: plots within a loop going out of margin when typesetting to PDF

The problem is that the generated .tex file has no spaces between the \includegraphics{} calls. LaTeX gives warnings about overfull hboxes, because the graphics aren't big enough to sit alone on a line, and are too big when it puts two on each line.

You can tell LaTeX (TeX really) to output the bad lines without putting two figures on each line by adding

\pretolerance=10000

in the text before the code chunk. You'll probably want to set it back to its default value

\pretolerance=100

after the code chunk, or LaTeX won't try hyphenation afterwards, and text can look really ugly.

Another way to fix this would be to force each figure to be in its own paragraph. You can do this by adding this code

my_plot_hook <- function(x, options)
paste("\n", knitr::hook_plot_tex(x, options), "\n")
knitr::knit_hooks$set(plot = my_plot_hook)

into a code chunk before you do your plotting. This puts a blank line
before and after each figure.



Related Topics



Leave a reply



Submit