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

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

问题描述

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

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

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

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.

我知道我可以直接使用乳胶并达到我想要的目的,但是我希望能够在需要时在Word中重现此内容.同样在Word中,图片上方和下方都有半页的空白空间.

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

推荐答案

您的问题与编织器无关,但与光栅图像有关,光栅图像产生白色边缘以防止扭曲.例如,如果键入? graphics::plot.raster,您将看到将asp参数设置为1保留栅格中的比率. 在标准R输出中绘制图像,您将看到空白部分,如果您调整窗口,这些空白部分将被删除.因此,您需要检查图像的尺寸,然后在针织机中使用fig.asp比率来生成框架,以使图像适合.

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.

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

这将返回

  format width height colorspace filesize
1    PNG   960    600       sRGB   762423

您也可以使用readPNG()

You can also use readPNG()

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

在knitr 选项中,我们有参数fig.asp

From knitr options we have the parameter fig.asp

fig.asp :(空值;数字)绘图的纵横比,即比例 高度/宽度;当指定fig.asp时,绘图的高度( 块选项fig.height)是从fig.width * 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

所以这里我们计算height/width = 0.62.

So here we calculate height/width= 0.62.

在这里,我使用的是平方输出作为第一个块中的自变量设置,这将在图像变宽时扩大问题.

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.

我知道问题中并未询问LATEX的答案,即使有些读者仍然 使用针织或编织来产生LATEX输出,那么,以下 显示了如何在编织物中修剪图像.使用的参数是 trim={<left> <lower> <right> <upper>,单位可以是厘米mm in ...(乳胶单位长度).要传递这些参数,可以在块选项中使用out.extra参数. 请注意,对上述图像使用fig.asp参数也可以.

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}

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

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

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

strip.white :(真;逻辑)是否删除行中的白线 输出中源块的开头或结尾

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天全站免登陆