繁体   English   中英

knitr:添加图形注释

[英]knitr: Add figure notes

我有一个看起来像这样的图:

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

现在我想在下面显示一组关于这个图的注释(即多行文本)。 在 knitr 创建的图形环境中,有没有什么方便的方法可以做到这一点?


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

我知道这是一个非常晚的答案,但这是我最终为同一类型的问题所做的。

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

# 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:

在此处输入图片说明

艾哈迈德的回答绝对惊人!

我对 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"

再次,非常感谢艾哈迈德!!

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

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

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

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

  • 最后,对于一些数字,我想使用 fig.pos="h!" 或者,Knitr / Latex 的 figure position = "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 = '')
    })

暂无
暂无

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

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