从knitr中的相同块/语句生成两个图 [英] Produce two plots from same chunk / statement in knitr

查看:86
本文介绍了从knitr中的相同块/语句生成两个图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从.Rmd文档中以不同的大小输出同一图形的两个版本的图生成代码?通过块选项(我在这里看不到任何直接起作用的东西)还是通过自定义knitr钩子?最好使用png设备完成此操作.

Is it possible to have plot-generating code output two versions of the same figure, at different sizes, from a .Rmd document? Either through chunk options (I didn't see anything that works directly here), or through a custom knitr hook? Preferably this would be done with the png device.

我的动机:我希望能够以一种尺寸输出一个图形,该图形可以内联在已编译的HTML文档中,而另一种图形则供用户单击后显示(请考虑fancybox ).我认为我将能够处理完成该工作所需的脚本.但是,首先,我需要说服R/knitr输出该图的两个版本.

My motivation: I'd like to be able to output a figure at one size, which would fit inline in a compiled HTML document, and another figure that a user could show after clicking (think fancybox). I think I'll be able to handle the scripting necessary to make that work; however, first I need to convince R / knitr to output two versions of the figure.

尽管我确定有解决方法,但最好是有某种方法可以使其幕后工作",例如通过knitr钩子.这样,我们不必对块中的R代码做任何特殊的事情,我们只需修改解析/评估该块的方式即可.

Although I'm sure there are workarounds, it would be best if there was some way to get it to 'just work' behind the scenes, e.g. through a knitr hook. That way, we don't have to do anything special to the R code within a chunk, we just modify how we parse / evaluate that chunk.

或者,可以使用可以很好地缩放的SVG图形,但随后我们就无法推断出图标签的大小合适,而矢量图形对于具有许多点的图也不是很好.

Alternatively, one could use SVG graphics that would scale nicely, but then we lose the nice inference of good sizes for the plot labels, and vector graphics aren't great for plots with many many points.

推荐答案

我认为没有解决方案,并且打算对@baptiste拒绝,但很快我就想到了一个黑手.以下是R Markdown示例:

I thought there was not a solution, and was about to say no to @baptiste, but got a hack in my mind soon. Below is an R Markdown example:

```{r test, dev='png', fig.ext=c('png', 'large.png'), fig.height=c(4, 10), fig.width=c(4, 10)}
library(ggplot2)
qplot(speed, dist, data=cars)
```

See the [original plot](figure/test.png) and
a [larger version](figure/test.large.png).

我认为dev的矢量化版本不起作用的原因是:对于dev=c('png', 'png'),第二个png文件将覆盖第一个png文件,因为图形文件名相同.然后我意识到fig.ext也是矢量化的,而像large.png这样的文件扩展名并没有真正破坏文件扩展名png;这就是为什么它是黑客.

The reason I thought the vectorized version of dev would not work was: for dev=c('png', 'png'), the second png file will overwrite the first one because the figure filename is the same. Then I realized fig.ext was also vectorized, and a file extension like large.png does not really destroy the file extension png; this is why it is a hack.

无论如何,通过devfig.extfig.heightfig.width的矢量化版本,您可以将同一图保存到多个版本.如果您对图形文件扩展名使用确定性模式,我想您还可以准备一些JavaScript代码,以将精美框自动附加到图像上.

Anyway, by vectorized versions of dev, fig.ext, fig.height, and fig.width, you can save the same plot to multiple versions. If you use a deterministic pattern for the figure file extensions, I think you can also cook up some JavaScript code to automatically attach fancy boxes onto images.

这篇关于从knitr中的相同块/语句生成两个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆