22
votes

I have a time-series dataset consisting of 10 variables.

I would like to create a time-series plot, where each 10 variable is plotted in different colors, over time, on the same graph. The values should be on the Y axis and the dates on the X axis.

Click Here for dataset csv

This is the (probably wrong) code I have been using:

c.o<-read.csv(file="co.csv",head=TRUE)
ggplot(c.o, aes(Year, a, b, c, d, e,f))+geom_line()

and here's what the output from the code looks like:

Can anyone point me in the right direction? I wasn't able to find anything in previous threads.

PROBLEM SOLVED, SEE BELOW.

One additional thing I would like to know:

Is it possible to add an extra line to the plot which represents the average of all variables across time, and have some smoothing below and above that line to represent individual variations?

1
you might need a separate question for your last comments. There are several ways to achieve this. One way would be have another level in variable for example called meana_f, you would then be plotting this with the same logic you would a or f in the current plot. - user1317221_G
Can you print head() of the initial .csv file, because the link you provided says file not found? - Nanami
Can you please provide the original dataset? - Eric
data set is missing - baxx

1 Answers

45
votes

If your data is called df something like this:

library(ggplot2)
library(reshape2)
meltdf <- melt(df,id="Year")
ggplot(meltdf,aes(x=Year,y=value,colour=variable,group=variable)) + geom_line()

enter image description here

So basically in my code when I use aes() im telling it the x-axis is Year, the y-axis is value and then the colour/grouping is by the variable.

The melt() function was to get your data in the format ggplot2 would like. One big column for year, etc.. which you then effectively split when you tell it to plot by separate lines for your variable.