【问题标题】:Incremental Plot in R Notebook ErrorR笔记本错误中的增量图
【发布时间】:2016-10-10 04:42:46
【问题描述】:

我正在尝试在 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 笔记本在评估第二个图时没有将前一个图保存在内存中。

这是有问题的 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 编译器?

【问题讨论】:

    标签: r rstudio knitr r-markdown rnotebook


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2023-02-08
      • 2023-03-24
      • 2021-09-28
      • 2017-07-04
      • 2015-05-12
      • 2018-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多