9

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

S12000
  • 3,345
  • 12
  • 35
  • 51

4 Answers4

10

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

Community
  • 1
  • 1
kirancodify
  • 695
  • 8
  • 14
8

If you use xts to create timeseries data, you can use plot.xts from xtsExtra package to get desired result

#Uncomment below lines to install required packages
#install.packages("xts")
#install.packages("xtsExtra", repos="http://R-Forge.R-project.org")

library(xts)
library(xtsExtra)

head(data)
##              ABC    DEF
## 2007-01-03 83.80 467.59
## 2007-01-04 85.66 483.26
## 2007-01-05 85.05 487.19
## 2007-01-08 85.47 483.58
## 2007-01-09 92.57 485.50
## 2007-01-10 97.00 489.46


plot.xts(data, screens = factor(1, 1), auto.legend = TRUE)

You will get something like this enter image description here

In case you want data in seperate panels:

plot.xts(data, auto.legend = TRUE)

enter image description here

CHP
  • 16,981
  • 4
  • 38
  • 57
  • this xts is very interesting and good looking but I get the following error when i try to install xtrsextra... (hope i can find an ohter alternative than taking an old R version...): Warning in install.packages : package ‘xtsExtra’ is not available (for R version 2.15.1) – S12000 Feb 28 '13 at 14:38
  • 1
    @Swiss12000 did you try `install.packages("xtsExtra", repos="http://R-Forge.R-project.org")` or just `install.packages("xtsExtra")` – CHP Feb 28 '13 at 15:58
  • The best answer, IMHO. `plot.ts` does not show x-axis labels as dates for one thing. Also there is not longer the `auto.legend = TRUE` option. Instead: `legend.loc = 'topleft'`. – James Hirschorn Aug 06 '17 at 03:03
7

Borrowing heavily from Jilber, I offer a slight variant. Here, the stress is on that it might be better to deal with actual time-series objects, since that will generally let you somewhat automatically get the types of output you might be interested in. Geektrader shows you how to do this with the "xts" package, but you can also do some similar things with base R.

Here's the modified version of Jilber's sample data, where I've converted it to a ts object.

set.seed(1)
DF <- data.frame(2000:2009,matrix(rnorm(50, 1000, 200), ncol=5))
colnames(DF) <- c('Year', paste0('Country', 2:ncol(DF)))
DF.TS <- ts(DF[-1], start = 2000, frequency = 1)
DF.TS
# Time Series:
# Start = 2000 
# End = 2009 
# Frequency = 1 
#       Country2  Country3  Country4  Country5  Country6
# 2000  874.7092 1302.3562 1183.7955 1271.7359  967.0953
# 2001 1036.7287 1077.9686 1156.4273  979.4425  949.3277
# 2002  832.8743  875.7519 1014.9130 1077.5343 1139.3927
# 2003 1319.0562  557.0600  602.1297  989.2390 1111.3326
# 2004 1065.9016 1224.9862 1123.9651  724.5881  862.2489
# 2005  835.9063  991.0133  988.7743  917.0011  858.5010
# 2006 1097.4858  996.7619  968.8409  921.1420 1072.9164
# 2007 1147.6649 1188.7672  705.8495  988.1373 1153.7066
# 2008 1115.1563 1164.2442  904.3700 1220.0051  977.5308
# 2009  938.9223 1118.7803 1083.5883 1152.6351 1176.2215

Now, here are two basic plotting options:

# Each country in a separate panel, no legends required
plot(DF.TS)

enter image description here

# All countries in one plot... colorful, common scale, and so on
plot(DF.TS, plot.type="single", col = 1:ncol(DF.TS))
legend("bottomleft", colnames(DF.TS), col=1:ncol(DF), lty=1, cex=.65)

enter image description here

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
5

What about this...?

> set.seed(1)
> DF <- data.frame(2000:2009,matrix(rnorm(50, 1000, 200), ncol=5))
> colnames(DF) <- c('Year', paste0('Country', 2:ncol(DF)))
> DF # this is how the data.frame looks like:
   Year  Country2  Country3  Country4  Country5  Country6
1  2000  874.7092 1302.3562 1183.7955 1271.7359  967.0953
2  2001 1036.7287 1077.9686 1156.4273  979.4425  949.3277
3  2002  832.8743  875.7519 1014.9130 1077.5343 1139.3927
4  2003 1319.0562  557.0600  602.1297  989.2390 1111.3326
5  2004 1065.9016 1224.9862 1123.9651  724.5881  862.2489
6  2005  835.9063  991.0133  988.7743  917.0011  858.5010
7  2006 1097.4858  996.7619  968.8409  921.1420 1072.9164
8  2007 1147.6649 1188.7672  705.8495  988.1373 1153.7066
9  2008 1115.1563 1164.2442  904.3700 1220.0051  977.5308
10 2009  938.9223 1118.7803 1083.5883 1152.6351 1176.2215
> matplot(DF[,-1], col=1:ncol(DF), type='l', lty=1, ylim=range(DF), axes=FALSE)
> axis(1, 1:nrow(DF), as.character(DF[,1]))
> axis(2)
> box() #- to make it look "as usual"
> legend('topright', names(DF), col=1:ncol(DF), lty=1, cex=.65)

enter image description here

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138