-1

I have a combined .csv file containing coordinates for multiple trajectories. I would like to plot these trajectories in R on the same graph with each line having a different colour (preferably using a loop). How do I do this?

  • 2
    you could start by adding some relevant sample data (using `dput`) to make your question reproducible. – Wimpel Dec 19 '18 at 17:57
  • Please take a look at [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), to modify your question, with a smaller sample taken from your data (check?dput()). Posting images of your data or no data makes it difficult to impossible for us to help you! – massisenergy Dec 19 '18 at 18:00

1 Answers1

0

Please supply a minimal working example for your work.

But in general: If you have loaded your .csv into R using read.csv or similar methods and arranged your data in a dataframe or matrix its a matter of looping over the desired dimension with lines. Example:

simdata <- function()
{
  set.seed(1234)
  data <- matrix(data=NA,nrow=10,ncol=100)
  for(i in 1:10) data[i,] <- dnorm(1:100,runif(1,1,100),runif(1,5,20))
  return(data)
}

Matrix <- simdata()

cols <- colorRampPalette(c("blue","red"))(10) #generate ramping colors
plot(NULL,xlim=c(0,ncol(Matrix)),ylim=range(Matrix)) #setup empty plot window
for(i in 1:nrow(Matrix)) lines(Matrix[i,],col=cols[i]) #plot
Julian_Hn
  • 2,086
  • 1
  • 8
  • 18