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)
```
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
How to Install the Odbc Driver for Snowflake Successfully on an M1 Apple Silicon MAC
Separate Ordering in Ggplot Facets
R Shiny Action Button and Data Table Output
Na Matches Na, But Is Not Equal to Na. Why
Remove Some of the Axis Labels in Ggplot Faceted Plots
How Does R Handle Object in Function Call
Pivot_Longer Multiple Variables of Different Kinds
How to Correctly 'Dput' a Fitted Linear Model (By 'Lm') to an Ascii File and Recreate It Later
How Many Elements in a Vector Are Greater Than X Without Using a Loop
How to Write Data from R to Postgresql Tables with an Autoincrementing Primary Key
Naive Bayes in Quanteda VS Caret: Wildly Different Results
Print the Sourced R File to an Appendix Using Sweave
Finding Unique Combinations Irrespective of Position
How to Hide/Toggle Legends Based on Addlayercontrol() in Leaflet for R
R: Find Missing Columns, Add to Data Frame If Missing
R - Set Execution Time Limit in Loop
Ubuntu 16.04 R Installation: Configure: Gdal-Config Not Found or Not Executable