0

I was trying to plot multiple time-series continuous variables in a single plot using ggplot2. As there were a lot of variable and I tried to use normal aesthetic mapping inside for-loop as,

p1<-ggplot(df, aes(x=timVar))
ind<-c(2,4,5,6,8,9,10,12,13,15,17) # Index of the series that I wanted to plot
for(i in ind){
    p1<-p1+geom_line(aes(df[,i]))
}
print(p1)

Since this gave me only the plot of last series and I googled for some solution and finally found one which had suggested me to use aes_string() function. I rebuild the code as,

p1<-ggplot(df, aes(x=timVar))
ind<-c(2,4,5,6,8,9,10,12,13,15,17) # Index of the series that I wanted to plot
for(i in ind){
    p1<-p1+geom_line(aes_string(names(df)[i]))
}
print(p1)

This gave me all the lines I needed. However, when I tried to get separate color for each variables, I could not get the discrete color. I used the following code,

p1<-p1+geom_line(aes_string(names(df)[i], col=names(df)[i]))

Is there any way to use aes_string and aes together inside the loop or is there any way to generate discrete color values with label to be the variable names.

TheRimalaya
  • 4,232
  • 2
  • 31
  • 37

1 Answers1

0

I am using the melt function of dplyr package. It solved all my problem above. Thanks for all your comments.

TheRimalaya
  • 4,232
  • 2
  • 31
  • 37