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

后端 未结 1 1086
醉话见心
醉话见心 2021-02-12 23:26

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.widt         


        
相关标签:
1条回答
  • 2021-02-13 00:11

    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.

    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}
    

    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

    0 讨论(0)
提交回复
热议问题