简体   繁体   English

针织衫-上面的图形标题

[英]knitr - Figure captions above

Is there a way, in knitr to move the fig.cap above the figure? 在针织机中,有没有办法将fig.cap移到图形上方? 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. 现在,标题和超链接目标位于底部,因此当选中标题时,页面的顶部仅显示标题,并且必须向上滚动才能看到该图……真是愚蠢。

Example of my current code: 我当前代码的示例:

<<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())
@

I understand there are ways to do so using latex and leaving fig.cap alone in the knitr chunk: 我知道有很多方法可以使用乳胶并在编织块中单独保留fig.cap:

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

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. 多数建议是执行上述操作,但日期可能在2012年或2013年初。想知道是否对knitr进行了任何更改,现在都可以使用此功能。

I've been using the options in knitr and xtable to control most things in my output but what is the consensus? 我一直在使用knitr和xtable中的选项来控制输出中的大多数内容,但是共识是什么? Should I avoid this and use latex options outside knitr chunks whenever possible? 我是否应该避免这种情况,并在可能的情况下在编织块外使用乳胶选项?

Maybe too late, but I think Yihui answered this. 也许为时已晚,但我想逸辉回答了这个问题。

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. 例如,这是.Rmd文档中块的版本。 If you tell knitr to change the option for figure to have the document look first at and then at the actual . 如果您告诉knitr更改图形选项,请首先查看文档,然后再查看实际文档。

```{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 = '')
})
```

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

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