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
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.
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.
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With