将 officer / office 中页面上的导入图像最大化到 Word (docx)

Maximize imported image on page in officer / officedown to Word (docx)

我最近尝试使用 officedown 创建文档的 .docx 报告。在我的文档中,我从项目目录中的 \images 文件夹导入图像。

通常当我编织文档时,我能够最大化它在页面上的位置。 有谁知道如何使用 officedown 来做到这一点?我在 RMarkdown

中 运行 这段代码时没有问题

这是我使用officedown得到的

这就是我想要的(注意图像占据了整个页面)

我在下面包含了一个代表

---
date: "`r Sys.Date()`"
author:
title: "GitHub Example"
output: 
  officedown::rdocx_document
---

```{r setup, include=FALSE, echo = FALSE}
pacman::p_load(readxl, dplyr, apastats, officedown, officer, ggplot2, knitr) # load packages

knitr::opts_chunk$set(fig.align = 'center',
                      fig.cap = TRUE,
                      fig.pos = 'H',
                      fig.path = 'images/',
                      echo = FALSE,
                      warning = FALSE, 
                      message = FALSE,
                      include = TRUE,
                      out.height="100%",  out.width="100%",
                      dpi = 300)

```

```{r}
# Creating a boxplot and saving it in \images directory
plot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_boxplot()
ggsave("images/plot.png",plot, width=11, height=8.5, dpi=300)

```

## Figures 

Figure \@ref(fig:boxplot) shows a boxplot that is made within the RMarkdown document. 
I want to call in an image saved from a previous R scripts which is saved 
in my `\images` directory shown in Figure \@ref(fig:plot). But notice how it 
does not take up the whole page.

<!---BLOCK_LANDSCAPE_START--->
```{r fig.cap="A boxplot", fig.id = "boxplot"}
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_boxplot()
```
<!---BLOCK_LANDSCAPE_STOP--->


<!---BLOCK_LANDSCAPE_START--->

```{r fig.id="plot", fig.cap="boxplot imported from images folder", echo=FALSE}
knitr::include_graphics("images/plot.png")
``` 

<!---BLOCK_LANDSCAPE_STOP--->

在 Word 输出中使用 plots/figures 的提示:
a) 你需要使用 fig.height and/or fig.width 来缩放 plots/figures;
b) 考虑使用块选项 crop = TRUE 和函数 hook_pdfcrop() 到 trim/crop the extra white margin around the plot (see @CL. SO answer here);
c) 块选项:Word 输出不支持 fig.alignfig.posout.heightout.widthout.extra

要获得理想的输出,您可以考虑进行以下更改:

```{r setup, include = FALSE, echo = FALSE}
pacman::p_load(readxl, dplyr, apastats, officedown, officer, ggplot2, knitr) # load packages

knitr::opts_chunk$set(fig.cap = TRUE,
                      fig.path = 'images/',
                      echo = FALSE,
                      warning = FALSE, 
                      message = FALSE,
                      include = TRUE,
                      dpi = 300)
```
```{r}
# Creating a boxplot and saving it in \images directory
plot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_boxplot()
ggsave("images/plot.png", plot, dpi = 300)
```
```{r fig.id = "plot", fig.cap = "boxplot imported from images folder", fig.height = 6, fig.width = 7.5, echo = FALSE}
knitr::include_graphics("images/plot.png")
```

这有什么帮助吗?

您可以使用 knitr 常用参数 fig.widthfig.height(英寸)。


---
date: "`r Sys.Date()`"
author:
title: "GitHub Example"
output: 
  officedown::rdocx_document
---

```{r setup, include=FALSE, echo = FALSE}
pacman::p_load(readxl, dplyr, apastats, officedown, officer, ggplot2, knitr) # load packages

knitr::opts_chunk$set(fig.align = 'center',
                      echo = FALSE,
                      warning = FALSE, 
                      message = FALSE,
                      dpi = 300)

```

```{r}
plot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_boxplot()
```



<!---BLOCK_LANDSCAPE_START--->
```{r fig.cap="A boxplot", fig.id = "boxplot"}
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_boxplot()
```

```{r fig.width=10, fig.height=5, fig.id="plot", fig.cap="boxplot imported from images folder", echo=FALSE}
plot
``` 

<!---BLOCK_LANDSCAPE_STOP--->

在为几个项目实施@David Gohel 回答之后。我仍然很羡慕在 PDF 输出中使用 out.heightout.width

在我的大多数项目中,我都使用文件资源管理器,右键单击有问题的图形并记下图像尺寸以计算其纵横比。

在典型的 officedown 项目中,我只会调用 fig.width,这样我就可以确保图形最大化到页面大小 ,而 保持其原始纵横比。

knitr::include_graphics("images/plot.png")

我努力尝试自动实现这一点,这需要读取图像尺寸来设置 fig.asp。它并不完美,我相信其他人会有更好更简洁的方法,但您可以使用 magick 包来完成此操作。这也需要 2 个区块来完成壮举(同样不是最优的)

{r, eval=TRUE, echo=FALSE}
# First chunk to fetch the image size and calculate its aspect ratio
img <- magick::image_read("images/plot1.png") # read the image using the magic library

img.asp <- image_info(img)$height / image_info(img)$width # calculate the figures aspect ratio
{r fig.width=11, fig.asp = img.asp, fig.id="plot", fig.cap="boxplot imported from images folder", echo=FALSE}
# second chunk uses "img.asp" to make sure our aspect ratio is maintained.
knitr::include_graphics("images/plot.png")