Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove white space above and below image in R Markdown?

I want to export a .Rmd file primarily as a latex pdf.

This is the code that I'm currently using

```{r ,fig.cap="caption",fig.env='figure', fig.width=10, fig.height=10,echo=FALSE, results='asis', warning=FALSE, strip.white=TRUE}
library(png)
library(grid)
img <- readPNG("filepath/overview.png")
grid.raster(img)
```

As you can see, I'm already using strip.white=TRUE & fig.env='figure' but they don't seem to work. The .PNG file hasn't got any (white) spacing above or below the image.

I know I can use latex directly and achieve what I want, but I want to able to reproduce this in Word if needed. Also in Word, there's half a page empty space above and below the image.

Any help would be greatly appreciated. Thanks

like image 409
Pdawg Avatar asked Sep 23 '15 19:09

Pdawg


1 Answers

Your problem is not linked with knitr but with the raster image, which produces a white edge to keep it from being distorted. For instance, if you type ? graphics::plot.raster you will see an asp argument set to 1 retain the ratio from the raster. Plot your image in a standard R output you will see the blank parts, if you adapt the window, those white parts are removed. So what you need is to check your image dimensions and then use a fig.asp ratio in knitr to produce a frame that will allow your image to fit.

Check your image ratio

Example using magick

url <- "https://cdn.pixabay.com/photo/2017/11/15/20/27/diamonds-2952447_960_720.png"
image<- image_read(url)
print(image) 

this returns

  format width height colorspace filesize
1    PNG   960    600       sRGB   762423

You can also use readPNG()

curl::curl_download(url, "image.png")
image <- png::readPNG("image.png",info=TRUE)
attr(image,"info")

From knitr options we have the parameter fig.asp

fig.asp: (NULL; numeric) the aspect ratio of the plot, i.e. the ratio of height/width; when fig.asp is specified, the height of a plot (the chunk option fig.height) is calculated from fig.width * fig.asp

So here we calculate height/width= 0.62.

Using knitr and docx output

Here I'm using a square output passed as an opts.label argument set in the first chunk, this will amplify the problem when the image is wide.

--- 
title: "Crop image ?" 
output: word_document
--- 

```{r echo=FALSE}
require(knitr)
library(magick)
opts_template$set(squarefigure = list(fig.height = 6, fig.width = 6))
```
=lorem()
```{r opts.label ="squarefigure", echo=FALSE, fig.cap = "plot without setting the right raster output size"}
url <- "https://cdn.pixabay.com/photo/2017/11/15/20/27/diamonds-2952447_960_720.png"
img <- image_read(url)
img%>%grid.raster()
```
=lorem()

```{r  opts.label ="squarefigure", fig.cap = "plot with correct margin, square size", fig.asp=0.62}
img%>%grid.raster()
```
=lorem()

As you can see the first image has a blank edge while the second is diplayed correctly.

docx output

Using LATEX with trim

I know the answer for LATEX was not asked in the question, still if some readers are using knitr or sweave to produce a LATEX output then, the following shows how to trim an image whithin knitr. The arguments used are trim={<left> <lower> <right> <upper> and the unit can be cm mm in ... (one of LATEX unit for length). To pass those arguments, you can use the out.extra argument in the chunk options. Note that using the fig.asp argument for your image like above will work too.

\documentclass{article}
\usepackage{graphicx}
\usepackage[english]{babel}
\usepackage{blindtext}
\begin{document}
\blindtext

<<r1, echo=FALSE >>=
library(knitr)    
library(ggplot2)
library(magick)
# download image to disk
url <- "https://cdn.pixabay.com/photo/2017/11/15/20/27/diamonds-2952447_960_720.png"
curl::curl_download(url, "image.png")
img <- image_read(png::readPNG("image.png"))
plot(img)
@

\blindtext
\newpage
\blindtext
<<r2, echo=FALSE,out.extra='trim={0 5cm 0 5cm},clip' >>=
plot(img)
@
\blindtext

\end{document}

enter image description here

Using HTML

Here is an excellent blog also for understanding arguments like fig.retina and out.width


Finally the strip.white argument is to remove blank lines of code. It will not resize your image.

strip.white: (TRUE; logical) whether to remove the white lines in the beginning or end of a source chunk in the output

like image 149
Cedric Avatar answered Nov 13 '22 13:11

Cedric