简体   繁体   English

knitr 中的图形标题和标签

[英]Figures captions and labels in knitr

I'm new to Knitr.我是 Knitr 的新手。 I'm trying to make a report using r chunks, and I can't figure out how to use captions and labels to reference the figure later on.我正在尝试使用 r 块制作报告,但我不知道如何使用标题和标签来稍后引用该图。 Here's an example of what I would like to do:这是我想做的一个例子:

---
title: "Plotting"

author: "xx"

date: '2015-08-10'

output: pdf_document
---
```{r figs, echo=FALSE, fig.width=7,fig.height=6,fig.cap="plotting example"}

    par(mfrow=c(2,2))
    plot(1:10, col=2)
    plot(density(runif(100, 0.0, 1.0)))
    plot(runif(100, 0.0, 1.0),type="l")
```

in Figure \ref{fig:figs} we see examples of plotting in R.

I would like to have a caption "Plotting example", and have a label, so I can use Figure \ref{fig.label} in the text.我想要一个标题“绘图示例”,并有一个 label,所以我可以在文本中使用图 \ref{fig.label}。 I have tried fig.cap and fig.lp, none of them works.我已经尝试过 fig.cap 和 fig.lp,它们都不起作用。 I would appreciate if if someone can help.如果有人可以提供帮助,我将不胜感激。

You can achieve this by including fig_caption: yes in the header: 您可以通过在标题中包含fig_caption: yes来实现此fig_caption: yes

---
title: "Plotting"
output:
  pdf_document:
    fig_caption: yes
---

```{r figs, echo=FALSE, fig.width=7,fig.height=6,fig.cap="\\label{fig:figs}plotting example"}
par(mfrow=c(2,2))
plot(1:10, col=2)
plot(density(runif(100, 0.0, 1.0)))
plot(runif(100, 0.0, 1.0),type="l")
```

in Figure \ref{fig:figs} we see examples of plotting in R.

点击这里查看截图

Note that the figure caption label should be included in the caption with a double backslash, as shown above. 请注意,图标题标签应包含在标题中,并带有双反斜杠,如上所示。

I also need to caption multiple figures in the same chunk, but this doesn't work.我还需要在同一块中为多个图形添加标题,但这不起作用。 Am I missing something?我错过了什么吗? I also found a solution of typing fig.cap = c("caption1","caption2").我还找到了输入 fig.cap = c("caption1","caption2") 的解决方案。 Hope that helps希望有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM