Generation of a large figure description using ggplot in R Markdown

Viewed 197

I need to have a long description that is attached to the bottom of my ggplot figure, is left aligned, and preferably I would like to do this directly in the R Markdown file. I am knitting to a PDF. Is there a package that just works for this kind of task?

This is the type of figure I am trying to replicate: enter image description here

Some things I have tried:

  1. Using the caption argument of labs():
ggplot(data, aes(x = x, y = y) +
  geom_point() +
  labs(caption = "Really long figure description")

This doesn't wrap the text.

  1. Using ggpubr::ggarrange:
p <- ggplot(data, aes(x = x, y = y) +
  geom_point() +
ggarrange(p, bottom = "Really long figure description")

This also doesn't wrap the text.

  1. Something like this in the chunk header:
{r, fig.cap = "\\label{fig:myfigure} Here be your caption text"}
    generate_a_figue(my_data)

This doesn't change the output at all.

Is there a way to do this that follows the R Markdown ethos of avoiding messing around with LaTeX files?

1 Answers

The long figure caption gets a bit clunky, but you can put the full thing in the header file as the fig.cap argument, as demonstrated below. The line break effect can be included with a twice escaped backslash: \\\\.

For bonus points, you can provide a short caption if you want to include that in the TOF:

```{r irisfig, echo=F, fig.cap='First line of a figure caption, \\\\ followed by an incredibly, incredibly, incredibly, incredibly, incredibly, incredibly, incredibly, incredibly, incredibly, incredibly, incredibly, incredibly, incredibly, incredibly, incredibly, incredibly, incredibly, incredibly, incredibly, incredibly, incredibly, incredibly, incredibly, incredibly, incredibly, long figure caption.'  , fig.scap='Short caption for the TOC'}
library(ggplot2)
ggplot2::ggplot(iris, aes(Sepal.Length, Sepal.Width))+
  geom_point()
```

Which yields:

enter image description here

Related