r - 如何在 R 中保存分层 pdf(通过 Sweave?)

标签 r pdf latex sweave

我搜索过,谷歌搜索,阅读 ?pdf ,并且对于将绘图保存为 pdf 的可能性感到枯燥,并且可以在 pdf 查看器边距中打开和关闭图层。我正在谈论的一个例子是 USGS 四边形地形图,可以下载为具有多层的 pdf,例如 this (压缩pdf)。

pdf() 帮助文件中的以下句子听起来不祥,但我也想检查一下我没有误解它:

 The R graphics model does not distinguish graphics objects at the level 
 of the driver interface.

我曾经能够在 Illustrator 中保存分层的 pdf,但不再有这个程序可供我使用。也许有人可以从 R 中想到一种解决方法?我用来映射的数据很大,但这是一个玩具示例:
pdf("2objects.pdf")
plot(NULL, type = "n",xlim = c(0,1),ylim = c(0,1))
rect(0,.7,.7,0,border = "blue",lwd=2)
rect(.3,1,1,.3,border = "red",lty=2,lwd=2)
dev.off()

它看起来像这样(它是一个 png,但上面会给出一个 pdf)
enter image description here

我希望能够将红色和蓝色框作为具有可见性的图层,可以在 pdf 查看器中打开和关闭。

非常感谢!

编辑:找到 thread in R-help (回复:@mnel),而且看起来不可能。我仍然会保留这个问题,以防有人想出一个漂亮的 R-tastic 解决方法。

编辑(2012 年 9 月 5 日):我尝试通过 Sweave 执行此操作,并使用发布的解决方法 here 取得了部分成功.此方法生成带有“图层”的单个 pdf,可以使用图像下方的超链接文本打开和关闭该图层。它使用“动画”技巧来做到这一点。虽然这仍然不是我最终想要的结果,但它的优点是不依赖于特定的 pdf 阅读器。我仍然会等着看是否有人在 Sweave 文档中发布了一种制作图层的方法,即 OCG,然后我可以将其自动化。

编辑(2012 年 9 月 13 日):我使用上面提到的代码发布了我的进度作为答案。我能够让它在更复杂的现实世界情况下工作,而无需更改代码,并在美国境内覆盖不同的行政和统计边界。在这种情况下,我只是将不同的 map 叠加命名为 layer-0.pdf , layer-1.pdf等,并且它没有错误地工作。我仍然希望最终会出现更好的东西。

谢谢大家的评论

最佳答案

看起来像 ( tex ) animation答案是我现在能想到的最好的答案。以下.Rnw文件将创建一个中间有一个图形的 pdf,它下面有 2 个文本超链接,它们分别切换红色和蓝色框的可见性。我找到了使这项工作起作用的 Tex 代码 here .我没有看过@Aaron 的 ocgtools建议,但会到达那里。谢谢大家的建议!

\documentclass{article}
%----------------------------------------------------------------%\
\usepackage[OT1]{fontenc}
\usepackage{Sweave}
\usepackage{animate}
\usepackage{hyperref}
\usepackage[margin=0.4in]{geometry}
%----------------------------------------------------------------%

\makeatletter
% command to create a toggle link
\newcommand{\ShowHideLayer}[3]{%
  % #1: anim No. (zero-based),
  % #2: layer No. (zero-based),
  % #3: link text
  \leavevmode%
  \pdfstartlink user {
    /Subtype /Link
    /Border [\@pdfborder]%
    /A <<
      /S/JavaScript
      /JS (
        \if at anim@useocg%
          if(a#1.fr[#2].state==true){
            a#1.fr[#2].state=false;
          }else{
            a#1.fr[#2].state=true;
          }
        \else
          if (a#1.fr[#2].display==display.visible){
            a#1.fr[#2].display=display.hidden;
          }else{
            a#1.fr[#2].display=display.visible;
          }
          this.dirty=false;
        \fi
      )
    >>
  }#3%
  \pdfendlink%
}

% command to create a link to show/hide all layers
\newcommand{\ShowHideAll}[2]{%
  % #1: anim No. (zero-based),
  % #2: link text
  \leavevmode%
  \pdfstartlink user {
    /Subtype /Link
    /Border [\@pdfborder]%
    /A <<
      /S/JavaScript
      /JS (
        var countvisible=0;
        for(var i in a#1.fr){
          \if at anim@useocg
            if(a#1.fr[i].state==true){countvisible++;}
          \else
            if (a#1.fr[i].display==display.visible){countvisible++;}
          \fi
        }
        if(countvisible){
          for(var i in a#1.fr){
            \if at anim@useocg
              a#1.fr[i].state=false;
            \else
              a#1.fr[i].display=display.hidden;
              this.dirty=false;
            \fi
          }
        }
        else{
          for(var i in a#1.fr){
            \if at anim@useocg
              a#1.fr[i].state=true;
            \else
              a#1.fr[i].display=display.visible;
              this.dirty=false;
            \fi
          }
        }
      )
    >>
  }#2%
  \pdfendlink%
}
\makeatother

\begin{document}

% heres the R-making of the plots, saved to working directory,
% which should be the folder containing this .Rnw file
% 3 versions of the same plot, one for each layer
<<echo = FALSE, hide = TRUE>>=
pdf("layer-0.pdf")
plot(NULL, type = "n", xlim = c(0, 1), ylim = c(0, 1), xlab = "", ylab = "")
dev.off()

pdf("layer-1.pdf")
plot(NULL, type = "n", xlim = c(0, 1), ylim = c(0, 1), axes = FALSE, xlab = "", ylab = "")
rect(0, .7, .7, 0, border = "blue", lwd = 2)
dev.off()

pdf("layer-2.pdf")
plot(NULL, type = "n", xlim = c(0, 1), ylim = c(0, 1), axes = FALSE, xlab = "", ylab = "")
rect(.3, 1, 1, .3, border = "red", lty = 2, lwd = 2)
dev.off()
@

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{center}
  %animated layer-set No. 0
  %                                          v-- frame rate ignored
  \animategraphics[width=1\linewidth,step]{1}{layer-}{0}{2}

  \ShowHideLayer{0}{1}{toggle red box}\\
  \ShowHideLayer{0}{2}{toggle blue box}\\
\end{center}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}

关于r - 如何在 R 中保存分层 pdf(通过 Sweave?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12269673/

相关文章:

linux - 如何在 Linux 命令行中将文本文件打印为 PDF 文件

python - 如何在 sympy 中正确渲染大括号?

templates - 如何使用 Emacs+AUCTeX 创建(自动插入)LaTeX 模板?

r - 在 Sweave 中使用 Latex 存储和稍后使用字幕的最佳方式?

r - 如何使用 R 和 XLConnect 保存 XLSX 电子表格而不在单元格中换行?

r - 是否有更优雅的方法将参差不齐的数据转换为整洁的数据框

objective-c - 根据个别页面显示从 URL 下载的 PDF

r - Plotly 颜色因子数据停止工作 - 错误包比例

r - 错误 "the condition has length > 1 and only the first element will be used"是什么意思?

ios - 将签名图像添加到 pdf 而不向 iOS 中的用户显示 pdf 数据