r - 在 R 中,如何自动注释每个保存的图形?

标签 r pdf graph annotations

我在 R session 中保存的图形和图表通常用于我的文档,我想用工作目录、文件名和日期对它们进行注释。由于我需要硬拷贝我的文档(不要问),这会让我的生活更轻松。我想我可以在打印之前修改 pdf,但实际上我更喜欢直接在 R 中盖印数字。

由于大多数时候我都是用 dev.copy2pdf() 生成图形,所以我编写了以下小函数:

# annotate PDF copy of the graph produced
copyan <- function( file= "tmp.pdf", cex= 0.75 ) {

  oldpar <- par( mar= c( 0, 0, 0, 0 ), usr= c( 0, 1, 0, 1 ) )
  on.exit( par( oldpar ) )

  par( new= TRUE )
  plot.new()

  # ann is the annotation stamp:
  # current working directory,
  # file name, date and time.
  ann <- paste( getwd(), file, Sys.time(), sep= ", " )
  strh <- strheight( ann, cex= cex )
  strw <- strwidth(  ann, cex= cex )

  # put text in the lower bottom corner,
  # just at the very margin of the plot
  usr1 <- par( "usr" )
  text( usr1[1] + strw/2, usr1[3] + strh/2, ann, cex= cex )

  dev.copy2pdf( file= file )
}

虽然它与我通常生成的绘图配合得相当好,但也许已经有更好的解决方案 OOB 了?或者,也许上面的脚本可以改进?

最佳答案

编写自己的函数来包装 dev.copy2pdf 是个好主意,我认为您正在努力解决这个问题。查看函数 mtexttitle 以了解在页边距中放置文本的不同方法。如果这些不能完全满足您的要求,请在设置 par(xpd=NA)< 后将 grconvertXgrconvertYtext 一起使用。在所有情况下,您可能希望使用 adj 参数来指定调整,而不是计算字符串宽度和高度并将值移动一半。

关于r - 在 R 中,如何自动注释每个保存的图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17294173/

相关文章:

algorithm - METIS 串行图分区器

r - data.table 余额变化的复利计算

r - 如何将 sec_axis() 用于 ggplot2 R 中的离散数据?

ruby-on-rails - Rails 3. 如何使用 ActiveAdmin 设置 Prawn?

javascript - 在 Internet Explorer 中从二进制文件打开 PDF

algorithm - 在有向加权图中找到两个节点之间的最短路径

r - 在 R 中使用公式作为关键字参数(命名参数)与 wilcox.test

R cv.glmnet 错误 : "need at least two non-NA values to interpolate"

c# - asp c# 创建动态pdf文件

python - 什么是 x 尺度和 y 尺度?