Search code examples
rrstudioknitrr-markdownrnotebook

Incremental Plot in R Notebook Error


I am trying to create an incremental HE plot in an R notebook that will be compiled to HTML (so a .R file). The function I am using heplot() from the heplots package uses an add = TRUE parameter to overlay the graphic over the previous one, which is useful when there are multiple groups you wish to compare.

If I run this as an R notebook, I get the following error:

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

I believe this is because the R notebook is not keeping the previous plot in memory when it evaluates the second plot.

Here is a reproducible example of the problematic R notebook file (save as .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))

I thought that maybe using a chunk option, such as fig.show="hold" might work, but this did not solve the issue.

If I knit this in an Rmarkdown document, it works as expected (save as .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))
```

So, my question is: how can I get the Rnotebook compiler to act similarly to the Rmarkdown compiler?


Solution

  • Apparently, my issue was very minor - my comment for the second HE plot created a new chunk, which is what caused the error.

    A working script simply removes the #' from the comment between the two calls to heplot()!