knitr: Show figure captions for multiple figures when setting figure dimensions using fig.width

Viewed 234

tldr: How can I show figure captions for chunks with multiple figures when the dimensions are set with fig.width and fig.height?

I'm trying to use rmarkdown to create a pdf. Some of the chunks produce multiple figures, but the caption won't display if I use the fig.width chunk option to set the figure size.

Caption is displayed with fig.width if the chunk produces one figure:

```{r demo_1, fig.width=3, fig.cap="I work because I produce one plot, even though I use fig.width"}
iris %>%
  ggplot(aes(x=Sepal.Length, y=Sepal.Width))+
  geom_point()
```

Caption is displayed with out.width if the chunk produces multiple figures:

```{r demo_2, out.width="50%", fig.show="hold", fig.cap="I work because I use out.width"}
iris %>%
  ggplot(aes(x=Sepal.Length, y=Sepal.Width))+
  geom_point()+
  labs(tag="A")

iris %>%
  ggplot(aes(x=Petal.Length, y=Petal.Width))+
  geom_point()+
  labs(tag="B")
```

Caption is not displayed with fig.width if the chunk produces multiple figures:

```{r demo_3, fig.width=3, fig.show="hold", fig.cap="I don't work because I set fig.width"}
iris %>%
  ggplot(aes(x=Sepal.Length, y=Sepal.Width))+
  geom_point()+
  labs(tag="A")


iris %>%
  ggplot(aes(x=Petal.Length, y=Petal.Width))+
  geom_point()+
  labs(tag="B")
```
0 Answers
Related