【问题标题】:R: plot.new has not been called yetR: plot.new 还没有被调用
【发布时间】:2019-03-27 12:08:11
【问题描述】:

我知道,这是一个非常古老的问题,在 plot.new has not been called yet 等中提到过。但是,那里的答案对我不起作用,所以我不得不再问一遍:

我正在阅读一个包含 30 行数据的简短表格,两个不同的表格:

lines <-scan("Wanna.txt", what="character", sep='\n')

结构如下:

AA BB
5  149
12 5
15 5
100 7
...
AA BB
5 1
10 136
23 150
100 3

然后我将表格读入数据结构:

Wanna5 <- read.table(textConnection(lines[1:5]), header=TRUE)
Wanna15 <- read.table(textConnection(lines[7:11]), header=TRUE)

当我做一个ggplot时,它可以工作

ggplot(data=Wanna5, mapping= aes(x=AA, y=BB)) + geom_line()

当我尝试添加简单的第二个数据集时

lines(Wanna15$AA, Wanna15$BB, type="l", col="green")

它告诉我旧的错误:

Error in plot.xy(xy.coords(x, y), type = type, ...) : 
plot.new has not been called yet

怎么办?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您似乎正在混合使用 ggplot 和基本 R 图。与其先创建第一个图,然后再添加行,不如用 ggplot 简单地创建整个图?这看起来像:

    ggplot() + geom_line(data=Wanna5, mapping= aes(x=AA, y=BB))
                 + geom_line(data = Wanna15, aes(x = AA, y = BB), 
                             col = 'green')
    

    这有帮助吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-17
      • 2016-07-26
      • 2017-07-26
      • 2017-02-02
      相关资源
      最近更新 更多