简体   繁体   English

绘制ggplot时出现“plot.new尚未被调用”错误

[英]“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

axis is part of the graphics package not ggplot. axis是图形 package 而不是 ggplot 的一部分。 So axis is looking for a plot not a ggplot .所以axis正在寻找plot而不是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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM