简体   繁体   English

R:plot.new尚未被调用

[英]R: plot.new has not been called yet

i know, it is an very old problem, mentioned in plot.new has not been called yet etc. but nevertheless, the answers there where not working for me so i have to ask it again: 我知道,这是一个非常老的问题,在plot.new中提到过,尚未被调用,等等。但是,那里的答案对我不起作用,所以我不得不再问一遍:

I am reading a short table with 30 lines with data, two different tables: 我正在读取一个短表,其中包含30行数据,两个不同的表:

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

It has the following structure: 它具有以下结构:

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

I then read the tables into a data structure: 然后,我将表读入数据结构:

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

When i do a ggplot, it works 当我做一个ggplot时,它有效

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

When i try to add the simple second data set 当我尝试添加简单的第二个数据集时

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

It tells me the old error: 它告诉我旧的错误:

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

What to do? 该怎么办?

It seems like you're mixing ggplot and base R plot. 似乎您正在混合ggplot和基准R图。 Instead of creating the first plot and then adding lines later, why don't you simply create the whole plot with ggplot? 为什么不使用ggplot简单地创建整个图,而不是先创建第一个图,然后再添加线? This would look like: 看起来像:

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

Does that help? 有帮助吗?

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

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