R markdown + bookdown + word output: how to place figure caption on top of figure

42 Views Asked by At

I am struggling with something simple: I want to generate a word document with a figure and add the figure caption on top (instead of at the bottom of the figure). Please have a look at example at the end of the post where the caption is at the bottom of the plot. I found several examples solving my problem, but they are with latex and pdf output. Any help is appreciated and I suspect this must be a one-liner.

Cheers

---
output: 
  bookdown::word_document2:
  fig_caption: yes
---

```{r, mydoc ,  echo=FALSE, eval=TRUE}


options(tidyverse.quiet = TRUE)


options(message = FALSE)


library(tidyverse, warn.conflicts = FALSE)


df<-tibble(x=seq(20), y=seq(20))


```{r spending, echo=FALSE,fig.cap="My caption bla bla.",fig.height = 6, fig.width = 12}

ggplot(data = df, aes(x  = x, y=y)) +
    geom_line()

``





1

There are 1 best solutions below

3
Grzegorz Sapijaszko On

Based on https://bookdown.org/yihui/rmarkdown-cookbook/word-officedown.html, you can use {officedown} like:

---
output:  
  officedown::rdocx_document: 
    plots:
      topcaption: true
---

options(tidyverse.quiet = TRUE)
options(message = FALSE)
library(tidyverse, warn.conflicts = FALSE)
library(officedown)
df<-tibble(x=seq(20), y=seq(20))

ggplot(data = df, aes(x  = x, y=y)) +
  geom_line()

which produces something like: