Coliban
Coliban

Reputation: 671

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:

I am reading a short table with 30 lines with data, two different tables:

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(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?

Upvotes: 1

Views: 2897

Answers (1)

BurlyPotatoMan
BurlyPotatoMan

Reputation: 194

It seems like you're mixing ggplot and base R plot. Instead of creating the first plot and then adding lines later, why don't you simply create the whole plot with 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?

Upvotes: 2

Related Questions