【问题标题】:knitr - Figure captions aboveknitr - 上面的图标题
【发布时间】:2014-03-11 20:08:12
【问题描述】:

有没有办法,在 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.

我当前的代码示例:

<<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 块中:

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

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

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

【问题讨论】:

  • 这 - stackoverflow.com/questions/16626462/… - 可能有一些你正在寻找的东西,但我认为你仍然被困在乳胶操作领域中。
  • @hrbrmstr 是的,我已经查看了该示例和许多其他示例。我最终使用了乳胶操作而不是 knitr 参数。从现在开始,我想我会为任何重叠的 knitr 参数使用乳胶。

标签: r latex knitr


【解决方案1】:

也许为时已晚,但我认为易辉回答了这个问题。

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-09-14
  • 2015-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 2018-10-14
相关资源
最近更新 更多