2

I am trying to achieve different heights and widths for subfigures in Rmarkdown. I was hoping that just providing fig.height and fig.width with a vector each would work, as this does seem to work for out.height and out.width

---
title: "Untitled"
output: pdf_document
header-includes:
   - \usepackage{subfig}
---



```{r, echo = FALSE, fig.height = c(1,2,3), fig.width=c(1,1,1), fig.cap='Caption', fig.subcap=c('Subcaption 1', 'Subcaption 2', 'Subcaption 3')}
library(ggplot2)
df <- data.frame(
  x = rnorm(30),
  y = rnorm(30)
)
p1 <- p2 <- p3 <- ggplot(df, aes(x, y)) +
  geom_point()

print(p1)

print(p2)

print(p3)
```

However, the result is this

enter image description here

So, it seems that all subfigures use the values fig.height = 3, fig.width= 1

Does somebody know how to specify those value for each subfigure separately?

Julian Karch
  • 444
  • 4
  • 20

1 Answers1

1

Those aren't really "subfigures", they are just side-by-side figures. So you can get what you want with some more typing:

```{r, echo = FALSE}
library(ggplot2)
df <- data.frame(
  x = rnorm(30),
  y = rnorm(30)
)
p1 <- p2 <- p3 <- ggplot(df, aes(x, y)) + geom_point()
```
```{r echo = FALSE, fig.height=1, fig.width=1}
print(p1)
```
```{r echo = FALSE, fig.height=2, fig.width=1}
print(p2)
```
```{r echo = FALSE, fig.height=3, fig.width=1}
print(p3)
```

You could probably automate this a little using ideas from https://yihui.org/knitr/demo/reference/, but I'm not sure it's worth the trouble. Another promising approach would be to use the gridExtra::grid.arrange function, though I'm not sure if it would allow the layout you want.

EDITED TO ADD after the comment indicating that true LaTeX subfigures are wanted:

This is harder, because as you saw, fig.height is not treated separately for each subfigure. I think you can get the heights you want by adding extra margins. To have full control over vertical centering, you need to turn off cropping of the figures using the YAML option

output: 
  pdf_document:
    fig_crop: FALSE

With that option this code

```{r, echo = FALSE, fig.height=3, fig.width=1,fig.subcap=c("first", "second", "third"),fig.cap="Main"}
library(ggplot2)
df <- data.frame(
  x = rnorm(30),
  y = rnorm(30)
)
p1 <- p2 <- p3 <- ggplot(df, aes(x, y)) + geom_point()
p1 + theme(plot.margin = margin(t = 1, b = 1, unit = "in") + theme_get()$plot.margin)
p2 + theme(plot.margin = margin(t = 1/2, b = 1/2, unit = "in") + theme_get()$plot.margin)
p3
```

gives this output:

screenshot

user2554330
  • 37,248
  • 4
  • 43
  • 90
  • Hey, thanks for your input! I realized that I dumbed down my problem too much. What I actually want in the end are true latex subfigures. I modified the original question to better reflect this. As far as I understand your code, this just creates three separate figures. The grid.arrange workaround is a good idea but, if possible, I would like to use latex to arrange the subfigures. – Julian Karch Apr 03 '20 at 18:37
  • I've posted a separate question https://stackoverflow.com/q/61029336/2554330 to find if there's a way to keep the bottom margins (in case you want them). – user2554330 Apr 04 '20 at 14:14
  • Found the answer to my referenced question, and will modify my answer to incorporate it. – user2554330 Apr 04 '20 at 16:04
  • I was able to use this answer plus creative use of theme(aspect.ratio = X) to get a pretty flexible option. – hartshoj Sep 26 '21 at 20:55