YQ.Wang
YQ.Wang

Reputation: 1177

plot thousands lines with ggplot

I'm tring to plot thousand lines (each line consists of 15 values) from data sequentially (the first line is from element NO.1 to NO.15; the second is from NO.16 to NO.30; ...) with ggplot. Here is a reproduceable code:

data<-data.frame(y=rnorm(95040))
p<-ggplot(data=data.frame(y=data[1:15,]),aes(x=c(1:15),y=y))+geom_line(alpha=I(1/7),size=1)
for(i in 1:(nrow(data)/15-1)){
  p<-p+geom_line(data=data.frame(y=data[(i*15+1):(i*15+15),]),aes(x=c(1:15),y=y),alpha=I(1/7),size=1)
}

When the loop runs at i=621, it reports an error:

Error: evaluation nested too deeply: infinite recursion / options(expressions=)?
Error during wrapup: evaluation nested too deeply: infinite recursion / options(expressions=)?

Why this happens and how to solve this plot problem (perhaps with another plot method)?

Upvotes: 1

Views: 1257

Answers (1)

Andrew Gustar
Andrew Gustar

Reputation: 18445

I think you need something like this...

data<-data.frame(y=rnorm(95040))
data$x <- 1:15
data$group <- (1:nrow(data)) %/% 15
p<-ggplot(data=data,aes(x=x,y=y,group=group))+geom_line(alpha=I(1/7),size=1)

Upvotes: 5

Related Questions