r - 如何在 R Markdown 中删除图像上方和下方的空白?

标签 r latex rstudio knitr r-markdown

我想主要将 .Rmd 文件导出为 latex pdf。

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

```{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 文件在图像上方或下方没有任何(白色)间距。

我知道我可以直接使用 latex 并实现我想要的,但如果需要,我希望能够在 Word 中重现它。同样在 Word 中,图像上方和下方都有半页空白区域。

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

最佳答案

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

检查您的图像比例

使用魔法的示例

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()
curl::curl_download(url, "image.png")
image <- png::readPNG("image.png",info=TRUE)
attr(image,"info")

来自 knitr options我们有参数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。

使用 knitr 和 docx 输出

这里我使用作为 opts.label 传递的方形输出。在第一个 block 中设置参数,当图像很宽时,这将放大问题。
--- 
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()

如您所见,第一张图像有空白边缘,而第二张图像正确显示。

docx output

使用带有修剪的 LATEX

我知道问题中没有问到 LATEX 的答案,但如果有些读者的话
正在使用 knitr 或 sweave 来产生一个 LATEX 输出然后,以下
显示如何在 knitr 中修剪图像。使用的论据是trim={<left> <lower> <right> <upper>单位可以是 cm mm in ...(长度为 LATEX unit 之一)。要传递这些参数,您可以使用 out.extra block 选项中的参数。
请注意,使用 fig.asp像上面那样为您的图像提供参数也可以。
\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

使用 HTML

Here也是一个很好的博客,也可以用来理解 fig.retina 之类的论点。和 out.width
最后是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 - 如何在 R Markdown 中删除图像上方和下方的空白?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32748235/

相关文章:

r - 在 Rstudio 中将 R 与 Spark 连接 - 启动 Spark shell 失败。端口文件不存在

c++ - Rcpp 库无法在 Ubuntu 上构建(找不到编译器)

latex - 相对截面规范

latex - 牛顿拉夫森法 LaTeX

python - 使用shell脚本或python检查目录内容是否发生变化

r - 在 mac 上为 windows 构建 R 包

r - 向量列表的所有可能组合

r - 如何将两列与数组组合

R:按 'two-way' 部分匹配过滤向量

r - 如何在 RStudio/R 中使用我的 AMD GPU 进行处理?