R笔记本错误中的增量图

时间:2016-06-09 13:32:49

标签: r rstudio knitr r-markdown rnotebook

我正在尝试在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))

我认为可能使用chunk选项,例如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编译器的行为类似?

1 个答案:

答案 0 :(得分:1)

显然,我的问题很小 - 我对第二个HE情节的评论创造了一个新的块,这是导致错误的原因。

工作脚本只会从对#'的两次调用之间的注释中删除heplot()