R 笔记本错误中的增量图
Incremental Plot in R Notebook Error
我正在尝试在 R 笔记本中创建一个增量 HE 图,该图将被编译为 HTML(因此是一个 .R 文件)。我使用 heplots
包中的 heplot()
函数使用 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 notebook 在评估第二个绘图时没有将前一个绘图保存在内存中。
这是有问题的 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 编译器?
显然,我的问题很小 - 我对第二个 HE 图的评论创建了一个新块,这就是导致错误的原因。
一个工作脚本只是从两次调用 heplot()
之间的注释中删除了 #'
!
我正在尝试在 R 笔记本中创建一个增量 HE 图,该图将被编译为 HTML(因此是一个 .R 文件)。我使用 heplots
包中的 heplot()
函数使用 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 notebook 在评估第二个绘图时没有将前一个绘图保存在内存中。
这是有问题的 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 编译器?
显然,我的问题很小 - 我对第二个 HE 图的评论创建了一个新块,这就是导致错误的原因。
一个工作脚本只是从两次调用 heplot()
之间的注释中删除了 #'
!