"Plot.new has not been called yet" - RMarkdown

**Update: Works in Rscript and when I run the entire chunk in Rmarkdown, but any idea as to why it doesnt work when I try to run the code line by line?

Hi there,

I am able to create a basic plot, but whenever I try to alter the x-axis labels I get the following error:

"Error in axis(1, seq(0, 1, 0.1)) : plot.new has not been called yet".

I would also like to label particular points on the plot (in particular, I would like the graph to label where a and b are on the graph.

 
a = 0.3 #lower bound of belief in theta  
  
b = 0.8 #upper bound of belief in theta

A = 5 ## monetary value of harm from

x = c(seq(0,1,by=0.05))  
  
  expfine = function(x,A,a,b){ifelse(x<=a, A, 
                              ifelse(a<x & x<b,(((b-x)/(b-a))*A),
                              ifelse(x>=b,0,NA)))}
expfine(x,A,a,b)
  
y = expfine(x,A,a,b)

 plot(y ~ x,main = expression("Expected Fine given x"), xlab = expression("Level of Effort (x)"),ylab = expression("Expfine"), type = "l",col = "red")               
  axis(1,seq(0,1,0.1))
  text(x = a, y = expfine(x=a,A,a,b),labels = "a")
  text(x = b, y = expfine(x=b,A,a,b),labels = "b")

Any thoughts on where I am going wrong?

Hi @NathanS,

I was able to copy and paste this code into an R Markdown file and run the code line-by-line, and also knit it to HTML without changing anything. So it works on my end. This makes me suspect that there is something specifically going on specific with your R session or how you're running things. Resetting R and starting over is usually a good first step in debugging.

I can reproduce your error if I "accidentally" run any of the last three lines prior to running the plot(...) function. This makes sense since axis() and text() are in reference to a plot that needs to exist. So perhaps you are running things out of order when doing it line-by-line?

1 Like

I'm sure that this is possible in base::plot, but this degree of control is what ggplot2 was built for. (And by the way, a and b are labelled, but too close to the plot line to be easily visible.)

suppressPackageStartupMessages(library(dplyr)) 
suppressPackageStartupMessages(library(ggplot2)) 
suppressPackageStartupMessages(library(tibble)) 

# changed assignment operator to <- from = because
# I like to distinguish creating new objects from
# assigning attributes to existing objects

a <-  0.3 #lower bound of belief in theta  
  
b <-  0.8 #upper bound of belief in theta

A <-  5 ## monetary value of harm from

x <-  c(seq(0,1,by=0.05)) 
  
expfine <-  function(x,A,a,b){ifelse(x<=a, A, 
                              ifelse(a<x & x<b,(((b-x)/(b-a))*A),
                              ifelse(x>=b,0,NA)))}

y <- expfine(x,A,a,b)

dat <- as.data.frame(cbind(x,y))

# set variables for elements; these can be set within theme;
# my preference is to set them outside for ease of editing
headline <- "Expected fine given x"
x_lab <- "Level of effort given x"
x_scale <- seq(0,1,0.1)
y_lab <- "Expfine"
l_col <- "red"
pt_a <- expfine(x=a,A,a,b)
pt_b <- expfine(x=b,A,a,b)
# create base layer
p <- ggplot(dat,aes(x,y)) +
# add title
ggtitle(headline) 
# create colored line layer
p + geom_line(color = l_col) + 
# labels for axes
  labs(x = x_lab, y = y_lab) + 
# adjust x scale
  scale_x_continuous(breaks = x_scale) +
# annotate inflection points
  annotate("text", x = a, y = pt_a - 0.25, label = "a") +
  annotate("text", x = b, y = pt_b + 0.25, label = "b") +
# apply minimal theme
  theme_minimal()

Created on 2020-02-29 by the reprex package (v0.3.0)

1 Like

Thank you for the response. I don't think I am running things out of order but I will try to reset the R session

Thank you very much!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.