I am trying to arrange three png images in a grid so that plot 1 takes up all of row 1, and plots 2 and 3 take up half each of row 2 giving me this sort of a configuration:
I managed it successfully using magick
and cowplot
.
However I am using the pdf output of R Markdown/bookdown and I want to be able to refer to them using the LaTeX subref package as \subref*{fig:label}
as done here. How do I achieve that in R Markdown?
Note that I know I can annotate the subfigures using cowplot but that would add the labels as rasterized images to the grid. For the sake of consistency with the rest of my document I would prefer it using 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))
```
Leaving the solution for visibility. In keeping with @Stéphane Laurent's advice, it's simpler to use LaTeX and insert:
\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}