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?

user1723765
  • 6,179
  • 18
  • 57
  • 85
  • 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 Nov 11 '12 at 17:38
  • 4
    Can you print head() of the initial .csv file, because the link you provided says file not found? – Nanami Aug 01 '13 at 06:29
  • 1
    Can you please provide the original dataset? – Eric Tobias Oct 29 '13 at 13:08
  • 1
    data set is missing – baxx Dec 05 '19 at 12:36

1 Answers1

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.

user1317221_G
  • 15,087
  • 3
  • 52
  • 78
  • 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 – user1723765 Nov 10 '12 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? – user1723765 Nov 10 '12 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 – user1317221_G Nov 10 '12 at 20:56
  • ok, but why do I get the error mentioned in my previous comment? – user1723765 Nov 11 '12 at 09:02
  • 2
    I missed something in the code you should use `meltdf<-melt(df,id="Year")`. I corrected this above too. – user1317221_G Nov 11 '12 at 10:04
  • 2
    Or you can use `geom_smooth`, possibly in combination with `geom_point` to get a smoother graph. – Paul Hiemstra Nov 11 '12 at 10:45
  • How could we plot each variable yearly and using variable as facet_wrap? – ah bon Oct 29 '21 at 10:00