Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting multiple time-series in ggplot

Tags:

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?

like image 765
user1723765 Avatar asked Nov 10 '12 16:11

user1723765


People also ask

How do I plot multiple time series in R?

Method 1: Using Basic R methods First, we create a data vector that has data for all the time series that have to be drawn. Then we plot the time series using the first dataset and plot() function. Then add other time series using line() function to the existing plot.

How do I plot multiple graphs in R?

We can put multiple graphs in a single plot by setting some graphical parameters with the help of par() function. R programming has a lot of graphical parameters which control the way our graphs are displayed. The par() function helps us in setting or inquiring about these parameters.


1 Answers

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.

like image 95
user1317221_G Avatar answered Sep 18 '22 10:09

user1317221_G