the loop is 1 thru 9 but the manual is just 1, 2, 3, 4? change the loop from 1:dim(mydata)[2] to 1:4 and it works :)
On Tue, Jun 24, 2014 at 9:43 AM, Zayd Farah <zaydfa...@gmail.com> wrote: > I hope the following script is reproducible enough to highlight my issue, > which is to automatically (in this case by loop, but it does not > necessarily have to be by a loop) add geom_line layers for each column of > mydata to the ggplot variable p1. for some reason doing this only works by > manually adding each layer: > > library(ggplot2) > library(gtable) > > > #creates set of reproducible data > xaxis<-rnorm(30)+10 > xaxis > mydata<-replicate(9, rnorm(30)+20) > > #creates a matrix of colors to identify each line on the final plot of > #each column of mydata > > colour_scheme<-palette(rainbow(dim(mydata)[2])) > > # Main scatterplot > p1 <- ggplot(NULL) > > #ideal code that would create a geom_line layer for each column of mydata > vs. xaxis > for (i in 1:dim(mydata)[2]) > { > p1=p1+geom_line(aes(xaxis,mydata[,i]),colour=colour_scheme[i]) > } > p1 > > #run the following code to see what it SHOULD look like: > p1 <- ggplot(NULL) > p1=p1+geom_line(aes(xaxis,mydata[,1]),colour=colour_scheme[1]) > p1=p1+geom_line(aes(xaxis,mydata[,2]),colour=colour_scheme[2]) > p1=p1+geom_line(aes(xaxis,mydata[,3]),colour=colour_scheme[3]) > p1=p1+geom_line(aes(xaxis,mydata[,4]),colour=colour_scheme[4]) > p1 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.