Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjusting figure margins in Rmarkdown

I am trying to adjust the piechart figure in my Rmarkdown document so that it spans the width of the page or at least becomes wide enough to fit all of the labels.

I have tried everything - adjusting figure.height and figure.width, margins with par(mar) and par(oma), but nothing seems to work. Either the pies themselves become smaller, or the labels are even more cut off. I would like the pies to be as large as possible with clearly visible labels, but every time it renders small pies and tiny labels.

Is there a workaround at least so that labels are not cut off (or can overlap the adjacent chart)? Any suggestions would be appreciated.

```{r, figure.align = "center", figure.height = 10, figure.width = 12}

par(mfrow=c(1,3), cex.axis=1.5, cex.lab=1.5) 
par(mar = c(4,2,4,2))
par(oma = c(3, 3, 3, 3))
pie(a, labels = lbls,  font = 2, col = c("tomato", "white"), cex=2)
pie(b, lbls2, font = 2, col = c("tomato", "white"), cex=2) 
mtext(side=3, text="Plan Breakdown: Top 20% of Users")
pie(c, lbls3,  font = 2, col = c("tomato", "white"))

enter image description here

like image 482
the_darkside Avatar asked Apr 03 '17 23:04

the_darkside


People also ask

How do I set margins in R plot?

To visualize how R creates plot margins, look at margin Figure 11.20. You can adjust the size of the margins by specifying a margin parameter using the syntax par(mar = c(bottom, left, top, right)) , where the arguments bottom , left … are the size of the margins. The default value for mar is c(5.1, 4.1, 4.1, 2.1).

What does Fig Cap do in R?

As in other Rmd documents, you can use the fig. cap chunk option to provide a figure caption, and adjust figure sizes using the fig. width and fig. height chunk options, which are specified in inches, and will be automatically scaled down to fit within the handout margin.

What is the default figure size R markdown?

height for R-generated figures only. Default is fig. width = 7 and fig. height = 5 (in inches, though actual width will depend on screen resolution).


3 Answers

Your figure size is constrained by the document margins unless you specify an out.width. If your figure width is larger than the margins of the page, then R Markdown/knitr will create a figure of the specified aspect ratio but shrink it down to fit within the margins.

To solve this, use out.width to set the width and height of the plot in the pdf. Something like:

```{r, fig.align = "center", fig.height = 8, fig.width = 8,
    out.width = "8.5in"}

pie(a, labels = lbls,  font = 2, col = c("tomato", "white"), cex=2)
pie(b, lbls2, font = 2, col = c("tomato", "white"), cex=2) 
mtext(side=3, text="Plan Breakdown: Top 20% of Users")
pie(c, lbls3,  font = 2, col = c("tomato", "white"))
````

See this page on knitr chunk options for more information.

like image 96
griseus Avatar answered Oct 18 '22 04:10

griseus


I had the same problem, seems some cropping is applyied by default, adding this in the yaml-header worked for me:

output: 
  pdf_document: 
    fig_crop: no
like image 37
snaut Avatar answered Oct 18 '22 03:10

snaut


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"))
    ```
like image 20
Fred Boehm Avatar answered Oct 18 '22 02:10

Fred Boehm