我前段时间开始使用 Sweave。然而,像大多数人一样,我很快就遇到了一个主要问题:速度。编织大型文档需要很长时间才能运行,这使得高效工作非常具有挑战性。使用 cacheSweave 可以极大地加速数据处理。但是,绘图 - 尤其是 ggplot ;) - 仍然需要很长时间才能渲染。这就是我想使用 pgfSweave 的方式。
经过很多很多小时后,我终于成功地使用 Eclipse/StatET/Texlipse 建立了一个工作系统。然后,我想将现有报告转换为与 pgfSweave 一起使用,但有一个严重的意外:我的大多数 ggplots 似乎不再工作了。例如,以下图在控制台和 Sweave 中完美运行:
pl <- ggplot(plot_info,aes(elevation,area))
pl <- pl + geom_point(aes(colour=que_id))
print(pl)
但是,使用 pgfSweave 运行它,我得到了这个错误:
Error in if (width > 0) { : missing value where TRUE/FALSE needed
In addition: Warning message:
In if (width > 0) { :
the condition has length > 1 and only the first element will be used
Error in driver$runcode(drobj, chunk, chunkopts) :
Error in if (width > 0) { : missing value where TRUE/FALSE needed
当我从 geom_point 中删除 aes(...) 时,该图与 pgfSweave 完美配合。
pl <- ggplot(plot_info,aes(elevation,area))
pl <- pl + geom_point()
print(pl)
编辑:我对问题进行了更多调查,可以将问题减少到 tikz-device。
这工作得很好:
quartz()
pl <- ggplot(plot_info,aes(elevation,area))
pl <- pl + geom_point(aes(colour=que_id))
print(pl)
这给出了上述错误:
tikz( 'myPlot.tex',standAlone = T )
pl <- ggplot(plot_info,aes(elevation,area))
pl <- pl + geom_point(aes(colour=que_id))
print(pl)
dev.off()
这也很好用:
tikz( 'myPlot.tex',standAlone = T )
pl <- ggplot(plot_info,aes(elevation,area))
pl <- pl + geom_point()
print(pl)
dev.off()
我可以用 5 个不同的 ggplots 重复这个。在映射中不使用颜色(或大小、alpha、...)时,它适用于 tikz。
Q1:有人对此行为有任何解释吗?
此外,非绘图代码块的缓存效果不佳。使用 Sweave,以下代码块完全不需要时间。使用 pgfSweave 大约需要 10 秒。
<<plot.opts,echo=FALSE,results=hide,cache=TRUE>>=
#colour and plot options are globally set
pal1 <- brewer.pal(8,"Set1")
pal_seq <- brewer.pal(8,"YlOrRd")
pal_seq <- c("steelblue1","tomato2")
opt1 <- opts(panel.grid.major = theme_line(colour = "white"),panel.grid.minor = theme_line(colour = "white"))
sca_fill_cont_opt <- scale_fill_continuous(low="steelblue1", high="tomato2")
ory <- geom_hline(yintercept=0,alpha=0.4,linetype=2)
orx <- geom_vline(xintercept=0,alpha=0.4,linetype=2)
ts1 <- 2.3
ts2 <- 2.5
ts3 <- 2.8
ps1 <- 6
offset_x <- function(x,y) 0.15*x/pmax(abs(x),abs(y))
offset_y <- function(x,y) 0.05*y/pmax(abs(x),abs(y))
plot_size <- 50*50
这似乎也是一个很奇怪的行为,因为只设置了一些变量供以后使用。
Q2:有人对此有任何解释吗?
Q3:更一般地说,我想问一下是否有人成功地使用了 pgfSweave?成功我的意思是所有在 Sweave 中工作的东西也可以在 pgfSweave 中工作,还有漂亮的字体和提高速度的额外好处。;)
非常感谢您的回复!