0

I have a data frame with N column (not know in advance) containing value of X_1, X_2, ... X_N per day. I need to be able to plot X_1, X_2, ... X_N

colors_list <- palette(rainbow(length(N)))
p <- ggplot(Store_Data.df, aes(x = Day)) + ylab(label="Value") + xlab("Time") + 
geom_line(aes(y = Data.df$V1, colour=colors_list[1])) +
geom_line(aes(y = Data.df$V2 colour=colors_list[2])) +
.
.
.
geom_line(aes(y = Data.df$V2 colour=colors_list[N]))

How can this be achieved for abitary N, i.e without having to hard wire the code. I've tried to loop over the column data, i.e.

 colors_list <- palette(rainbow(length(N)))
p <- ggplot(Store_Data.df, aes(x = Day)) + ylab(label="Value") + xlab("Time") 
for (i in 1:N){
    p <- p + geom_line(aes(y = Data.df$[,i], colour=colors_list[i]))
}

but the plot only shows the last set of values, i.e y = Data.df$[,N]. How can this be done?

user2350366
  • 491
  • 1
  • 4
  • 20
  • Please see [here](http://stackoverflow.com/questions/5963269) about making a reproducible example as a guide to help us answer your questions. – Erdem Akkas Apr 24 '17 at 11:28

1 Answers1

0

ggplot only allows for one column to be assigned as y variable because it is based on the long format, rather than the wide format.

To get what you want the easiest way is to reshape your data to long format, then, group by colours.

Here is a quick example using the swiss dataset in R and the melt function from the reshape package.

require(reshape2)
swiss_soldiers<-swiss #data in wide format
swiss_soldiers<- melt(swiss_soldiers, "Fertility") #Reshape to long format, using "Fertility" as x variable
head(swiss_soldiers)
  Fertility    variable value
1      80.2 Agriculture  17.0
2      83.1 Agriculture  45.1
3      92.5 Agriculture  39.7
4      85.8 Agriculture  36.5
5      76.9 Agriculture  43.5
6      76.1 Agriculture  35.3
ggplot(swiss_soldiers)+aes(x=Fertility, y=value, colour=variable)+geom_point()+geom_smooth(method = "lm")
#A graph containing the individual data as points plus a linear trendline

A graph containing the individual data as points plus a linear trendline

In this way you don't even need your loop.

Maarten Punt
  • 256
  • 1
  • 11