2

我正在尝试在 R 笔记本中创建一个增量 HE 图,该图将被编译为 HTML(因此是一个 .R 文件)。我heplot()heplots包中使用的函数使用一个add = TRUE参数将图形覆盖在前一个图形上,这在您希望比较多个组时很有用。

如果我将其作为 R 笔记本运行,则会收到以下错误:

Error in polygon(E.ellipse, col = last(fill.col), border = last(col),  : 
  plot.new has not been called yet
Calls: <Anonymous> ... withVisible -> eval -> eval -> heplot -> heplot.mlm -> polygon

我相信这是因为 R 笔记本在评估第二个图时没有将前一个图保存在内存中。

这是有问题的 R 笔记本文件的可重现示例(另存为 .R):

#' ---
#' title: "Incremental HE Plots Test"
#' author: "Matthew Sigal"
#' date: "08 Jun 2016"
#' ---

#' ## Load package and data:
library(heplots)
data(Rohwer, package="heplots")

#' ## Multivariate models for two subsets:
rohwer.ses1 <- lm(cbind(SAT, PPVT, Raven) ~ n + s + ns + na + ss, 
                  data = Rohwer, subset = SES == "Hi")
rohwer.ses2 <- lm(cbind(SAT, PPVT, Raven) ~ n + s + ns + na + ss, 
                  data = Rohwer, subset = SES == "Lo")

#' ## Overlaid visualization:
heplot(rohwer.ses2, col = c("red", rep("black",5), "blue"), 
       hypotheses = list("B=0, Low SES" = c("n", "s", "ns", "na", "ss")), 
       level = 0.5, cex = 1.25, 
       fill = c(TRUE, FALSE), fill.alpha = 0.05, 
       xlim = c(-15, 110), ylim = c(40, 110),
       label.pos = c(1, rep(NULL, 5), 1))

#' ## High SES students:
heplot(rohwer.ses1, col = c("red", rep("black", 5), "blue"), 
       hypotheses = list("B=0, High SES" = c("n", "s", "ns", "na", "ss")), 
       level = 0.5, cex = 1.25, 
       add = TRUE,   # place both plots on same graphic
       error = TRUE, # error ellipse is not drawn by default with add = TRUE
       fill = c(TRUE, FALSE), fill.alpha = 0.05, 
       xlim = c(-15, 110), ylim = c(40, 110))

我认为可能使用块选项,例如fig.show="hold"可能有效,但这并没有解决问题。

如果我在 Rmarkdown 文档中编织它,它会按预期工作(另存为 .Rmd):

---
title: "Rmd Test"
author: "Matthew Sigal"
date: "June 9, 2016"
output: html_document
---

## Test

```{r}
library(heplots)
data(Rohwer, package="heplots")
rohwer.ses1 <- lm(cbind(SAT, PPVT, Raven) ~ n + s + ns + na + ss, 
                  data = Rohwer, subset = SES == "Hi")
rohwer.ses2 <- lm(cbind(SAT, PPVT, Raven) ~ n + s + ns + na + ss, 
                  data = Rohwer, subset = SES == "Lo")

heplot(rohwer.ses2, col = c("red", rep("black",5), "blue"), 
       hypotheses = list("B=0, Low SES" = c("n", "s", "ns", "na", "ss")), 
       level = 0.5, cex = 1.25, 
       fill = c(TRUE, FALSE), fill.alpha = 0.05, 
       xlim = c(-15, 110), ylim = c(40, 110),
       label.pos = c(1, rep(NULL, 5), 1))
heplot(rohwer.ses1, col = c("red", rep("black", 5), "blue"), 
       hypotheses = list("B=0, High SES" = c("n", "s", "ns", "na", "ss")), 
       level = 0.5, cex = 1.25, 
       add = TRUE,   # place both plots on same graphic
       error = TRUE, # error ellipse is not drawn by default with add = TRUE
       fill = c(TRUE, FALSE), fill.alpha = 0.05, 
       xlim = c(-15, 110), ylim = c(40, 110))
```

所以,我的问题是:我怎样才能让 Rnotebook 编译器的行为类似于 Rmarkdown 编译器?

4

1 回答 1

1

显然,我的问题非常小——我对第二个 HE 情节的评论创建了一个新块,这就是导致错误的原因。

#'一个工作脚本只是从两次调用之间的注释中删除heplot()!

于 2016-06-09T13:34:59.760 回答