Kevin Lee
Kevin Lee

Reputation: 351

"plot.new has not been called yet" error when graphing ggplot

Why does this happen/How can I troubleshoot this? Iv read some of the other entries about this error but am still confused, especially because when I run the following code without the bolded part, it runs fine, but with the bolded section included, i get this error.

Code:

ggplot(diamonds, aes(x = price)) + geom_histogram(binwidth = 500) + 
axis(side = 1, at = seq(0, 20000, by = 500))

Error:

Error in axis(side = 1, at = seq(0, 20000, by = 500)) : 
  plot.new has not been called yet

Upvotes: 0

Views: 360

Answers (1)

NotThatKindODr
NotThatKindODr

Reputation: 719

axis is part of the graphics package not ggplot. So axis is looking for a plot not a ggplot.

Try

ggplot(diamonds, aes(x = price)) + 
  geom_histogram(binwidth = 500) +
  scale_x_continuous(breaks = seq(0,20000, by = 500)) 

Or in base graphics

hist(diamonds$price) 
axis(side = 1, at = seq(0, 20000, by = 500))

Upvotes: 1

Related Questions