在R Markdown中在网格中排列不同纵横比的png

问题描述 投票:0回答:1

我试图在网格中排列三个 png 图像,以便图 1 占据第 1 行的全部,图 2 和图 3 占据第 2 行各一半,从而给我这样的配置:

我使用

magick
cowplot
成功管理了它。

但是,我正在使用 R Markdown/bookdown 的 pdf 输出,并且我希望能够使用 LaTeX subref 包作为

\subref*{fig:label}
来引用它们,就像这里一样。如何在 R Markdown 中实现这一目标?

请注意,我知道我可以使用cowplot注释子图,但这会将标签作为光栅化图像添加到网格中。为了与我的文档的其余部分保持一致,我更喜欢使用 markdown。

---
title: "Grid"
output:
  bookdown::pdf_document2:
    template: NULL
    keep_tex: true
    number_sections: false
    latex_engine: pdflatex
header-includes:
  - \usepackage[subrefformat=simple]{subfig}

--- 
```{r label, echo = FALSE, warning=FALSE}
#| fig.cap: "(a) Figure1 (b) Figure2 (c) Figure3"
#| fig.align: "center"
#| fig.subcap: !expr c("", "", "")
library(cowplot)
library(magick)
library(ggplot)

# Load the PNG image
png_image1 <- image_read("images/rectangle.png")
png_image2 <- image_read("images/square1.png")
png_image3 <- image_read("images/square2.png")

# Create a ggplot2 plot using the image
im1 <- image_ggplot(png_image1)
im2 <- image_ggplot(png_image2)
im3 <-  image_ggplot(png_image3)

#Plot grid
row1 <- plot_grid(im1, nrow = 1, ncol = 1)
row2 <- plot_grid(im2, im3, nrow = 1, ncol = 2)
plot_grid(row1, row2, nrow = 2, rel_heights = c(1, 1))
```
r latex r-markdown bookdown cowplot
1个回答
0
投票

将解决方案留给可见性。根据 @Stéphane Laurent 的建议,使用 LaTeX 并插入会更简单:

\begin{figure}[H]
    \subfloat[\label{fig:label-1}]
    {\includegraphics[width=1\linewidth]{images/rectangle}}\\
    \subfloat[\label{fig:label-2}]
    {\includegraphics[width=.48\linewidth]{images/square1.png}}
    \subfloat[\label{fig:label-3}]
    {\includegraphics[width=.48\linewidth]{images/square2.png}}\hfill
    \caption{Caption}\label{fig:label}
\end{figure}
© www.soinside.com 2019 - 2024. All rights reserved.