knitr - 上面的图标题 [英] knitr - Figure captions above

查看:17
本文介绍了knitr - 上面的图标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法,在 knitr 中将 fig.cap 移动到图形上方?我在上面需要它们,以便当为某个表格选择图表列表的超链接时,它会导航到该图表.Right now the caption and thus hyperlink target are at the bottom so when selected only the caption is shown at the top of the page and one must scroll up to see the figure...rather silly.

Is there a way, in knitr to move the fig.cap above the figure? I need them above so that when the list-of-figure's hyperlink is selected for a certain table it navigates to the figure. Right now the caption and thus hyperlink target are at the bottom so when selected only the caption is shown at the top of the page and one must scroll up to see the figure...rather silly.

我当前的代码示例:

<<Race, fig.lp='fig:', fig=TRUE, eval=TRUE, echo=FALSE, dev='png', fig.pos='H', fig.width=8, fig.height=4, fig.cap='Race', fig.align='center', dpi=300>>=
b <- ggplot(melt(race.plot, id=c("Type"), variable.name="Race", value.name="Count"))
b + geom_bar(aes(Race, y=Count, fill=Race), position="dodge", stat="identity", alpha=0.7) + 
  ggtitle("Race") + xlab("") + ylab("Count") +
  facet_wrap(~Type, nrow=2, scale="free_y") +
  theme(plot.title=element_text(size=25), 
    axis.title=element_text(size=15), 
    axis.text.y=element_text(size=10),
    axis.text.x=element_blank(),
    axis.ticks.x=element_blank())
@

我知道有一些方法可以使用乳胶并将 fig.cap 单独留在 knitr 块中:

I understand there are ways to do so using latex and leaving fig.cap alone in the knitr chunk:

egin{figure} 
caption{This is a caption above the figure} 
<<a-plot, echo=FALSE>>= 
plot(1) 
@ 
end{figure} 

大多数建议是执行上述操作,但日期大约是 2012 年或 2013 年初.我想知道现在对 knitr 的任何更改是否允许此功能.

Most suggestions are to do the above, but date around 2012 or early 2013. Am wondering if any changes to knitr allow this functionality now.

我一直在使用 knitr 和 xtable 中的选项来控制输出中的大部分内容,但共识是什么?我是否应该尽可能避免这种情况并在 knitr 块之外使用乳胶选项?

I've been using the options in knitr and xtable to control most things in my output but what is the consensus? Should I avoid this and use latex options outside knitr chunks whenever possible?

推荐答案

也许为时已晚,但我认为宜慧回答了这个问题.

Maybe too late, but I think Yihui answered this.

您可以更改所有图形的编织钩以在图形上方显示标题.例如,这里是来自 .Rmd 文档的块的一个版本.如果您告诉 knitr 更改 figure 的选项,让文档先查看,然后再查看实际的 .

You can change the knit hook for all figures to have the caption above the figure. For example, here is a version of a chunk from an .Rmd document. If you tell knitr to change the option for figure to have the document look first at and then at the actual .

```{r setup}
library(knitr)
knit_hooks$set(plot = function(x, options) {
  paste('<figure><figcaption>', options$fig.cap, '</figcaption><img src="',
        opts_knit$get('base.url'), paste(x, collapse = '.'),
        '"></figure>',
        sep = '')
})
```

这篇关于knitr - 上面的图标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆