我搜索过,谷歌搜索,阅读 ?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)
我希望能够将红色和蓝色框作为具有可见性的图层,可以在 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/