如何在knitr中使用网格逐步构建分层图? [英] How to build a layered plot step by step using grid in knitr?

查看:74
本文介绍了如何在knitr中使用网格逐步构建分层图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于与研讨会相关的在线教程,我想强调一下网格包的使用(尤其是如何使用视口).为此,我想一步一步地构建一个绘图(即逐块).在每个步骤/块之间,我想包括一些普通的文本,以便更详细地解释每个步骤.

For an online tutorial accompanying a workshop, I would like to highlight the use of the grid package (especially how to work with viewports). For this, I would like to build a plot step by step (i.e. chunk by chunk). Between each of the steps/chunks I would like to include some ordinary text in order to explain each of the steps in more detail.

如何告诉knitr不要分别评估一个块,而是在上一个块结束的地方开始评估?基本上,我不是想对块进行新的评估,而是要添加到先前块的结果中.

How can I tell knitr to not evaluate a chunk separately, but to start the evaluation where the previous chunk ended? Basically, rather than a new evaluation of the chunk I want to add to the result of the previous chunk.

在下面的代码中,发生的事情是当编织为html时,我在.html输出中得到了2个图.第一个显示第一个块的结果(一个粉红色的矩形和一些文本),第二个显示第二个块的结果(一个蓝色的矩形).我要实现的是两个图-第一个图显示第一个块的结果(如上所述),第二个图显示第一个块的结果+第二个块的结果(粉红色矩形内的蓝色矩形) ). 基本上,我想在R控制台中运行时重现两个代码块的行为.蓝色矩形应放置在粉红色矩形中,而不要分别绘制.

In the below code what happens is that I get 2 plots in the .html output when knitting to html. The first one showing the resutls of the first chunk (a pink rectangle and some text) and the second showing the results of the second chunk (a blue rectangle). What I would like to achieve is two plots - the first one showing the results of the first chunk (as above) and the second plot showing the results of the first chunk + the results of the second chunk (the blue rectangle within the pink rectangle). Basically, I would like to reproduce the behavior of the two code chunks when run in the R console. The blue rectangle should be placed in the pink rectangle and not be plotted separately.

这是第一块

```{r grid first vp, tidy = FALSE}
library(grid)
grid.newpage()

## draw a rectangle around the root vp and provide some text
grid.rect()
grid.text("this is the root vp", x = 0.5, y = 1, just = c("centre", "top"))

vp <- viewport(x = 0.5, y = 0.5, 
               height = 0.5, width = 0.5,
               just = c("centre", "centre"))

pushViewport(vp)

grid.rect(gp = gpar(fill = "pink"))
grid.text("this is our first vp", x = 0.5, y = 1, just = c("centre", "top"))
```

然后在它们之间添加一些说明文字:

Then some explanatory text in between:

好,所以现在我们在root视口的中间,在x = 0.5y = 0.5-just = c("centre", "centre")处创建了一个视口,该视口是原始视口-width = 0.5.

"Ok, so now we have created a viewport in the middle of the root viewport at x = 0.5 and y = 0.5 - just = c("centre", "centre") that is half the height and half the width of the original viewport - height = 0.5 and width = 0.5.

此后,我们导航到该视口-pushViewport(vp),然后绘制了一个填充整个视口的矩形并将其填充为粉红色-grid.rect(gp = gpar(fill = "pink"))

Afterwards we navigated into this viewport - pushViewport(vp) and then we have drawn a rectangle that fills the entire viewport and filled it in pink colour - grid.rect(gp = gpar(fill = "pink"))

请注意,我们尚未离开视口.这意味着,无论我们现在执行什么操作,都将在当前活动的视口(粉红色的视口)中发生.为了说明这一点,我们将再次简单地从上面重复完全相同的代码(我们将只更改填充颜色,以便更好地看到更改)."

Note that we didn't leave the viewport yet. This means, whatever we do now, will happen in the currently active viewport (the pink one). To illustrate this, we will simply repeat the exact same code from above once more (we're only going to change the fill colour so we see the change better)."

这是第二块

```{r grid second vp, tidy = FALSE}

vp <- viewport(x = 0.5, y = 0.5, 
               height = 0.5, width = 0.5,
               just = c("centre", "centre"))

pushViewport(vp)

grid.rect(gp = gpar(fill = "cornflowerblue"))
```

有什么想法可以告诉knitr保持"在先前块中所做的一切,并将其作为当前块评估的起点"?

Any ideas how I could tell knitr to 'keep' whatever has been done in previous chunks and take this as the 'starting point' for the current chunk evaluation?

推荐答案

它没有记录,但是此功能已经存在了将近一年.要使图形设备在整个编译过程中保持打开状态,您可以设置

It is not documented, but this feature has been there for almost a year. To keep a graphical device open throughout the compilation, you can set

opts_knit$set(global.device = TRUE)

我认为任何人都很少使用此功能,但是似乎我错了.

I thought it would be very rare for anybody to use this feature, but it seems I was wrong.

这篇关于如何在knitr中使用网格逐步构建分层图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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