Show caption only in list of figures in Rmarkdown document - hide it for figures themselves

Hi,

Rmardown is not capable of getting the caption title from ggplot object's title or caption (it can do that for kable's table caption for example). So if I want a figure listed in the list of figures(lof) on the top of document, the only way to achieve it is by putting the caption inside chunk header's fig.cap. But I still prefer the ggplot original title to figure caption. + I am missing an option to put a subtitle to the plot if I go the fig.cap way.

The best way for me would be to retain the ggplot title and to have the figure caption displayed only in the lof on top of document. It there a way to do that? Out of desperation, I tried to change the fig.capcolor to white but it made it invisible in both the lof and the figure itself.

Thanks!

Edit: Example added

---
title: "Getting rid of the caption: The story"
subtitle: "today"
author: "me"
output:
  bookdown::pdf_document2:
    extra_dependencies:
    - float
toc: true
lot: true
lof: true
toc_depth: 2
df_print: kable
header-includes:
  \usepackage{float}
  \let\origfigure\figure
  \let\endorigfigure\endfigure
  \renewenvironment{figure}[1][2] {
      \expandafter\origfigure\expandafter[H]
  } {
      \endorigfigure
  }
include-before:
- \stepcounter{page}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=FALSE, error=FALSE, warning=FALSE, warning=FALSE, message=FALSE)
library(ggplot2)
library(dplyr)

data(iris)
iris %>%
  ggplot(aes(Sepal.Length, Sepal.Width, color = Species)) + 
  geom_point() +
  ggtitle("My preferred  title", subtitle = "My preferred subtitle") +
  labs(caption = "This caption does not work with the lof")

I don't think there is a mechanism to have a List of Figure but no caption below a figure. The two comes together, especially because of the numbering. Usually you add fig.cap to create the caption, and this will be taken into the list of figure. fig.scap can be used to modify the text used in the LOF, useful when caption is too long.

What format are you targetting ? PDF ?

You are seeking a specific mechanism for ggplot2 to reuse some of its component with the mechanism in knitr. This would be a feature request got ggplot2 maybe. But even if caption in ggplot2 could be used as figure caption in knitr, it would as a caption and not as text inside the saved plot (because it would not be possible for other tools to generate the LOF).

Maybe what you can do is create a list of figure by you're own by saving the titles in an object, and creating markdown content in the output to present the list of all you figure. Usually for PDF you rely on latex to do that but you could make a R function to help you maybe.

Hope it helps

1 Like

Thank you Christophe. I was worried this would be the case. I was just wondering if anyone came up with some clever workaround. So I guess it comes down to the good old Sophie's Choice for me.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.