2013-02-28 96 views
7

enter image description here用圖例繪製R中的倍數(時間)系列

根據我的數據(比較圖片)稱爲GDP。我想知道如何在一個圖表中繪製所有國家的圖表。我想爲每個國家/地區獲得一個圖例,例如每行不同顏色或每行不同形狀。

我知道如何繪製一個系列,例如:

ts.plot(GDP $ ALB)

但不知道如何繪製所有系列的傳奇人物。

謝謝

+0

你應該採樣數據。 – 2013-02-28 09:35:20

+0

@geektrader你好,對不起,你能給我更多的細節我不明白。 – S12000 2013-02-28 09:36:18

+0

獲得理想情節的一種方法是使用ggplot2,如[這裏]所示(http:// stackoverflow。com/questions/13324004/plotting-multiple-time-series-in-ggplot) – 2013-02-28 09:40:22

回答

6

如果使用xts創建時間序列數據,你可以使用plot.xtsxtsExtra包,從而獲得所需結果

#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) 

你會得到這樣的事情enter image description here

如果你想在獨立的面板數據:

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

enter image description here

+1

爲xts替代+1。 – A5C1D2H2I1M1N2O1R2T1 2013-02-28 10:17:15

+0

這xts是非常有趣,很好看,但我得到以下錯誤,當我嘗試安裝xtrsextra ...(希望我可以找到比採取舊的R版本更爲歐特...):警告在install.packages: package'xtsExtra'不可用(對於R版本2.15.1) – S12000 2013-02-28 14:38:27

+1

@ Swiss12000是否嘗試過'install.packages(「xtsExtra」,repos =「http://R-Forge.R-project.org」)或只是'install.packages(「xtsExtra」)' – 2013-02-28 15:58:47

4

這是怎麼回事?

> 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

+1

+1上覆制以提供其他人可以使用的框架;) – A5C1D2H2I1M1N2O1R2T1 2013-02-28 10:16:34

+0

關鍵是要認識到軸必須分開繪製。 – jnas 2015-04-02 13:37:01

5

從Jilber大量舉債,我提供了一個輕微的變形。這裏的重點在於,處理實際的時間序列對象可能會更好,因爲這通常會讓您自動獲得您可能感興趣的輸出類型。Geektrader將向您展示如何使用「xts 「包,但你也可以做一些類似的事情與基地R. R

這裏是Jilber的示例數據的修改後的版本,我已經將它轉換爲一個ts對象。

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 

現在,這裏有兩個基本的繪圖選項:

# 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

+0

感謝您的評論和upvote我的答案;)+1爲您的替代 – 2013-02-28 11:09:07

6

在短短的2號線採用ts.plot

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

結果:簡介倍數(時間)系列中的R與圖例 Plot multiples (time) series in R with legend