user341191
user341191

Reputation: 13

Referencing issue for Rmarkdown

I'm trying to link figures throughout my R Markdown file and I keep getting the error when I try to knit the profile:

[WARNING] Citeproc: citation Picture not found

Further, when I try to cite using @chunk-name, the output produces a result such as chunk-name? with a question mark.

I have tried to solve this error by downloading pandoc due to some internet searching but absolutely nothing has helped me. Anything regarding this error message would be helpful.

I have attached a code of my YAML if it's of any help:

Apologies I don't know how to attach it as a code without the lines coalescing.

Example of GG Plot I was trying to create:

```{r Picture , fig.asp=0.5, out.width= "100%", warning=FALSE, message=FALSE, fig.cap= " Student's grade count vs planning "}

library(ggplot2)

ggplot(qn1frame,  aes(x=(hourly_plan), fill=category
)) + geom_bar() + theme_bw(base_size = 12) + scale_fill_brewer(palette = "Set1") +
    labs(fill = "Grades", y = "Proportion", x = "ewuhrwe") + plot_layout(guides = 'collect') +
  plot_annotation(tag_levels = 'A')

ggplot(qn1frame,  aes(x=(hourly_plan), fill=category
)) + geom_bar(aes(stat="identity"), position= "fill") + theme_bw(base_size = 12) + scale_fill_brewer(palette = "Set1") +
    labs(fill = "Grades", y = "Proportion", x = "Occupational exposure (yrs)") + plot_layout(guides = 'collect') + plot_annotation(tag_levels = 'A')

```

How I've been trying to create a citation back to this graph:

Testing captions: 

Referring to @Picture.. 

However, I get the WARNING output when I render the file as well as in the actual code I get: (Picture?) when I tag it. Any help would be immensely helpful.

Thank you!

Upvotes: 0

Views: 348

Answers (1)

Shafee
Shafee

Reputation: 19857

You are having the question mark sign ?? instead of a reference because you are trying to refer from a code chunk that generates two figures. So when you use `@ref(fig:chunk-name) to refer to the figures, it gets confused as to which figure to refer to.

So one option could be using a separate chunk for each plot. Or if you want to generate multiple plots from the same chunk, you need to refer them with \@ref(fig:chunk-name-1), \@ref(fig:chunk-name-2) etc.

Reproducible Example

---
title: "Cross Referencing"
output:
  bookdown::html_document2:
    self_contained: yes
    code_folding: hide
    code_download: yes
    toc: yes
    toc_float: yes
    number_sections: yes
    fig_caption: TRUE
link-citations: true
link-references: true
date: "2022-09-16"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

library(ggplot2)
```

## R Markdown

```{r}
#| cars-plot, 
#| fig.cap=c("Displacement vs Miles per gallon", "HP vs Miles per gallon"), 
#| echo=FALSE,
#| fig.height=3,
#| fig.width=4

ggplot(mtcars, aes(mpg, disp)) +
  geom_point()

ggplot(mtcars, aes(mpg, hp)) +
  geom_point()
```


Testing captions: 

Referring to \@ref(fig:cars-plot-1) and \@ref(fig:cars-plot-2)

referencing multiple plots from same code chunk


Upvotes: 1

Related Questions