Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot multiples (time) series in R with legend

Tags:

r

time-series

enter image description here

According to my datas (cf. picture) called GDP. I would like to know how to plot all the countries in one graph. And I would like to get a legend for each countries like different colours per line or different shape per line.

I know how to plot one series, for instance:

ts.plot(GDP$ALB)

But don't know how to plot all series with a legend.

Thank you

like image 902
S12000 Avatar asked Feb 28 '13 09:02

S12000


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 you plot multiple curves on the same graph in R?

To draw multiple curves in one plot, different functions are created separately and the curve() function is called repeatedly for each curve function. The call for every other curve() function except for the first one should have added an attribute set to TRUE so that multiple curves can be added to the same plot.

What is Lty in legend in R?

line type (lty) can be specified using either text (“blank”, “solid”, “dashed”, “dotted”, “dotdash”, “longdash”, “twodash”) or number (0, 1, 2, 3, 4, 5, 6).


1 Answers

In just 2 lines using ts.plot

    ts.plot(time,gpars= list(col=rainbow(10)))
    legend("topleft", legend = 1:10, col = 1:10, lty = 1)

Result: Plot multiples (time) series in R with legend Plot multiples (time) series in R with legend

like image 58
kirancodify Avatar answered Sep 28 '22 11:09

kirancodify