【问题标题】:knitr: Add figure notesknitr:添加图形注释
【发布时间】:2016-07-03 00:07:55
【问题描述】:

我有一个像这样的图:

<<foo, fig.lp='', fig.cap='name', fig.subcap=c('left', 'right'),>>=
plot1
plot2
@

现在我想在正下方显示一组关于此图的注释(即多行文本)。在 knitr 创建的图形环境中是否有任何便捷的方法可以做到这一点?


正如上面 cmets 中已经指出的,目前我的问题没有解决方案。我已提交功能请求。

【问题讨论】:

  • 标题、子标题或只是在数字下方写文字有什么问题?
  • 文字应该放在标题之外(就像表格中的笔记)。
  • 更准确地说:字幕不起作用,因为我可能决定在字幕周围移动,但我仍然希望注释在图的正下方。出于同样的原因,副标题不起作用。在图形下方写文字不起作用,因为当图形移动时,不能保证图形和文本会保持在一起(在 LaTeX 中)。
  • 你在搞this之类的东西,对吧?由于knitr 没有提供在figure 环境中添加内容的内置方法,我的猜测是您必须隐藏图形(fig.show = "hide")并手动包含它,以及所需的LaTeX 标记。
  • 你可以file a feature request。但是我建议不要使用“这很遗憾”之类的措辞,而是“这将是一个有价值的功能......因为......”。

标签: r latex knitr figure subfigure


【解决方案1】:

我知道这是一个很晚的答案,但这是我最终针对同一类型的问题所做的。

我定义了一个自定义钩子,可以根据需要绘制图像

# Custom knitr hook to add notes to the plot
knit_hooks$set(plot = function(x, options) {
  paste("\n\\end{kframe}\n\\begin{figure}\n",
        "\\includegraphics[width=\\maxwidth]{",
        opts_knit$get("base.url"), paste(x, collapse = "."),
        "}\n",
        "\\textsc{Note} -- here is some car stuff with notes",
        "\\caption{", options$fig.cap, "}\n",
        "\n\\end{figure}\n\\begin{kframe}\n",
        sep = '')
})

这是完整的 .Rnw

\documentclass{article}

\usepackage[font=large,labelfont=sc]{caption}

\begin{document}

<<setup, echo=FALSE, message=FALSE, results='hide'>>=
suppressPackageStartupMessages({
  library(ggplot2)
})

opts_chunk$set(echo=FALSE)
opts_chunk$set(results="hide")
@

<<foo, fig.cap='with notes', fig.height=4, fig.width=6>>=
# save a regular plotting function
regular_plot <- knit_hooks$get("plot")

# Custom knitr hook to add notes to the plot
knit_hooks$set(plot = function(x, options) {
  paste("\n\\end{kframe}\n\\begin{figure}\n",
        "\\includegraphics[width=\\maxwidth]{",
        opts_knit$get("base.url"), paste(x, collapse = "."),
        "}\n",
        "\\textsc{Note} -- here is some car stuff with notes",
        "\\caption{", options$fig.cap, "}\n",
        "\n\\end{figure}\n\\begin{kframe}\n",
        sep = '')
})

ggplot(data = mtcars) + geom_point(aes(disp,mpg))
@

<<bar, fig.cap='without notes', fig.height=4, fig.width=6>>=
# restore regular plotting function
knit_hooks$set(plot = regular_plot)

ggplot(data = mtcars) + geom_point(aes(disp,mpg))
@

\end{document}

这是生成的 PDF:

【讨论】:

    【解决方案2】:

    akhmed 的回答绝对令人惊叹!

    我对 Rmd 进行了轻微修改,以首先获取标题并概括文档中的每个块。

    我们必须在开头添加这些行:

    knit_hooks$set(plot = function(x, options, .notes = notes, .sources = sources) {
      paste("\n\n\\begin{figure}\n",
            "\\includegraphics[width=\\maxwidth]{",
            opts_knit$get("base.url"), paste(x, collapse = "."),
            "}\n",
            "\\caption{",options$fig.cap,"}","\\label{fig:",opts_current$get("label"),"}","\\textsc{}",
            "\n\\textsc{Notas} -- ",.notes,
            "\n\\textsc{Fuentes} -- ", .sources,
            "\n\\end{figure}\n",
            sep = '')
    })
    

    那么在每一块我们只写剧情的注释和来源

    notes = "Notes to explain the plot"
    sources = "Explain the sources"
    

    再次,非常感谢 akhmed!

    Pd:我使用"\\textsc{}" 在标题、注释和来源之间生成一个空格。

    最好将这一点概括为在同一图中使用具有许多数字的子标题。

    【讨论】:

    • 当我尝试{r, message=FALSE, plot=TRUE, fig.cap="Fig1", notes="test notes", sources = "some source"} 只有两个块(一个用于钩子,另一个用于绘图)时,它会崩溃并显示错误消息未使用的参数(`NA`=NULL, `NA`=NULL, `NA`=NULL)
    【解决方案3】:

    @akhmed 的解决方案对我非常有帮助。我需要做一些额外的定制,我将其作为答案传递(评论太长了)。

    • 首先,我希望对笔记的边距进行更多控制,并发现添加 minipage 环境会有所帮助(\\begin{minipage} 下面设置为 6 英寸宽)。
    • 其次,我通过设置字体大小和左对齐文本添加了一些小的格式添加(\\small\\begin{flushleft} 下面)。

    • 最后,对于一些数字,我想使用 fig.pos="h!"或图形位置 = Knitr / Latex 的“here”选项,我花了一分钟才意识到这个钩子覆盖了那个块选项,所以我手动将它添加为\\begin{figure}[h!]

    再次感谢@akhmed 提供此解决方案。

    knit_hooks$set(plot = function(x, options) {
        paste("\n\\end{kframe}\n\\begin{figure}[h!]\n",
          "\\includegraphics[width=\\maxwidth]{",
          opts_knit$get("base.url"), paste(x, collapse = "."),
          "}\n",
          "\\begin{minipage}{6in}\\small\\begin{flushleft}Note: Lorem ipsum \\end{flushleft}\\end{minipage}",
          "\\caption{", options$fig.cap, " \\label{", options$fig.lp, opts_current$get("label"), "}}\n",
          "\n\\end{figure}\n\\begin{kframe}\n",
          sep = '')
        })
    

    【讨论】:

      猜你喜欢
      • 2017-10-30
      • 1970-01-01
      • 2020-08-26
      • 2021-10-11
      • 2012-02-27
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      相关资源
      最近更新 更多