用图例在 R 中绘制倍数(时间)序列 [英] Plot multiples (time) series in R with legend
问题描述
根据我的数据(参见图片)称为 GDP.我想知道如何在一张图中绘制所有国家/地区.我想为每个国家/地区获得一个图例,例如每条线不同的颜色或每条线的不同形状.
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)
ts.plot(GDP$ALB)
但不知道如何用图例绘制所有系列.
But don't know how to plot all series with a legend.
谢谢
推荐答案
如果您使用 xts
创建时间序列数据,您可以使用 中的
包以获得所需的结果plot.xts
xtsExtra
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)
你会得到这样的东西
如果您需要单独面板中的数据:
In case you want data in seperate panels:
plot.xts(data, auto.legend = TRUE)
这篇关于用图例在 R 中绘制倍数(时间)序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!