24

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?

4
  • 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. Nov 11, 2012 at 17:38
  • 4
    Can you print head() of the initial .csv file, because the link you provided says file not found?
    – Nanami
    Aug 1, 2013 at 6:29
  • 1
    Can you please provide the original dataset? Oct 29, 2013 at 13:08
  • 1
    data set is missing
    – baxx
    Dec 5, 2019 at 12:36

1 Answer 1

49

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.

7
  • for some reason I get the following error: Error in eval(expr, envir, enclos) : object 'Year' not found and before this when I added the melt() command it said: > meltdf=melt(c.o) Using as id variables Nov 10, 2012 at 18:47
  • also is there a way to make the lines look 'nicer'? these abrupt jumps look a bit like they were confidence intervals...can it be made a bit more continuous? Nov 10, 2012 at 19:00
  • the jumps are your data.. you could take means for the years within groups i.e. cast() after melt(), plot a smoothed function of the data or coarsen the year scale one the x axis but that is really up to you and what you are trying to achieve Nov 10, 2012 at 20:56
  • 2
    I missed something in the code you should use meltdf<-melt(df,id="Year"). I corrected this above too. Nov 11, 2012 at 10:04
  • 2
    Or you can use geom_smooth, possibly in combination with geom_point to get a smoother graph. Nov 11, 2012 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.