如何在 R Markdown 中删除图像上方和下方的空白? [英] How to remove white space above and below image in R Markdown?

查看:82
本文介绍了如何在 R Markdown 中删除图像上方和下方的空白?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想主要将 .Rmd 文件导出为乳胶 pdf.

这是我目前正在使用的代码

```{r ,fig.cap="caption",fig.env='figure', fig.width=10, fig.height=10,echo=FALSE, results='asis',警告=假,strip.white=真}图书馆(png)图书馆(网格)img <- readPNG("文件路径/overview.png")grid.raster(img)```

如您所见,我已经在使用 strip.white=TRUE &fig.env='figure' 但它们似乎不起作用..PNG 文件在图像上方或下方没有任何(白色)间距.

我知道我可以直接使用乳胶并实现我想要的,但如果需要,我希望能够在 Word 中重现这一点.同样在 Word 中,图片上下都有半页空白.

任何帮助将不胜感激.谢谢

解决方案

您的问题与 knitr 无关,而是与光栅图像有关,光栅图像会产生白边以防止失真.例如,如果您键入 ?graphics::plot.raster 你会看到一个 asp 参数设置为 1 保留光栅的比率.在标准 R 输出中绘制您的图像,您将看到空白部分,如果您调整窗口,这些白色部分将被删除.所以你需要检查你的图像尺寸,然后在 knitr 中使用 fig.asp 比例来生成一个框架,让你的图像适合.

检查您的图像比例

使用魔法的示例

url <-https://cdn.pixabay.com/photo/2017/11/15/20/27/diamonds-2952447_960_720.png"图片<- image_read(url)打印(图像)

返回

 格式宽度高度颜色空间文件大小1 PNG 960 600 sRGB 762423

你也可以使用 readPNG()

curl::curl_download(url, "image.png")图片 <- png::readPNG("image.png",info=TRUE)属性(图像,信息")

从knitr

使用带有修剪的 LATEX

我知道问题中没有问到 LATEX 的答案,但如果有些读者正在使用 knitr 或 sweave 来产生一个 LATEX 输出然后,以下显示如何在 knitr 中修剪图像.使用的论据是trim={<left><下><对><upper> 单位可以是 cm mm in ...(LATEX

使用 HTML

Here 是一个很好的博客,也可以用于理解 fig.retinaout.width

等参数<小时>

最后,strip.white 参数是删除空白代码行.它不会调整您的图像大小.

<块引用>

strip.white: (TRUE;logical) 是否去掉里面的白线输出中源块的开头或结尾

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

解决方案

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}
egin{document}
lindtext

<<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)
@

lindtext

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

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

这篇关于如何在 R Markdown 中删除图像上方和下方的空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆