0

I used any of the following codes to merge two of my data frames:

join(j5, c4, inner")
merge(j5, c4, left_on='year', how='inner')

The resultant data frame being:

   year tradevaluejapan tradevaluechina
1  1992         1215216          265860
2  1993          654958          300987
3  1994         1058199          693368
4  1995         4827964         1608935
5  1996         2882890         1457594
6  1997         5859965          880888
7  1998         3499868          859137
8  1999         3530975         1069889
9  2000          439580         2026727
10 2001         9808706         2675847
11 2002         9358647         4568524
12 2003         1348892         4608139
13 2004          945569         6619693
14 2005          738876        14812109
15 2006         1453525        17870811
16 2007         9335089        19904723
17 2008         5391225        26881686
18 2009           55391        18815007
19 2010         1246401        20065842
20 2011           25783        19004412
21 2012           88171        17278793
22 2013          329110        19116949
23 2014         1066022        32076571

Separately, I learnt how to plot a multiple time series using the codes from the following link: Plot multiples (time) series in R with legend

But how can I plot my resultant merged data frame as a multiple time series?

Community
  • 1
  • 1
Roamaa
  • 1
  • 1
  • thanks akrun but i used the same link to find answers on how to merge my data frame. But I'd like to know how I can plot the resultant multiple time series data frame. – Roamaa May 31 '15 at 05:47
  • Sorry, I was just reading your title and thought it might help – akrun May 31 '15 at 05:48
  • Your merged dataset have two value columns and a year column, wouldn't the `plot.xts` in the link work – akrun May 31 '15 at 05:49
  • i think it should but the thing is, after i merge my dataframes, it doesnt come up as another dataframe. the result just appears on console. which kind of makes it difficult for me to use plot.xts – Roamaa May 31 '15 at 05:53
  • `df1 <- merge(j5, c4, left_on='year', how='inner'); xt1 <- xts(df1[-1], order.by=as.Date(paste0(df1$year,'-01-01')));plot.xts(xt1)` seems to work – akrun May 31 '15 at 05:55
  • ill try that. thanks – Roamaa May 31 '15 at 05:56
  • xt1 <- xts(df1[-1], order.by=as.Date(paste0(df1$year,'-01-01')));plot.xts(xt1) There were 50 or more warnings (use warnings() to see the first 50) Warning message: In plot.xts(xt1) : only the univariate series will be plotted – Roamaa May 31 '15 at 06:05
  • this is what happens when i try to plot it. what should i do? – Roamaa May 31 '15 at 06:06
  • I am not getting the warning with the example you posted using `xts_0.9-7` and `R 3.2.0` – akrun May 31 '15 at 06:06
  • I guess you need `library(xtsExtra)` I had it already loaded. If you don't load it, you will get that warning. The link to download the package is https://r-forge.r-project.org/R/?group_id=118 or use `install.packages("xtsExtra", repos="http://R-Forge.R-project.org", type="source")` – akrun May 31 '15 at 06:13
  • I thought that as well. And i tried loading xtsExtra, but apparently their isnt any xtsExtra in my version of R. I just have xts package. – Roamaa May 31 '15 at 06:15
  • I updated the previous comment. Can you try that command to install it and then load xtsExtra i.e. `library(xtsExtra)` – akrun May 31 '15 at 06:16
  • install.packages("xtsExtra", repos="http://R-Forge.R-project.org") Installing package into ‘C:/Users/ods00846/Documents/R/win-library/3.2’ (as ‘lib’ is unspecified) Warning in install.packages : package ‘xtsExtra’ is not available (for R version 3.2.0) – Roamaa May 31 '15 at 06:17
  • this is what i have been getting every time i try that command – Roamaa May 31 '15 at 06:18
  • I don't know when I got it installed. It must have been in my previous R version which got upgraded. Can you try a different repos? i.e. `repos=http://cran.rstudio.com` Also, in the rforge link, there is an option to download the zip file for windows and then build it. – akrun May 31 '15 at 06:26
  • I shall definitely try that later. For the time being I used the following commands and it worked - DF.TS <- ts(df1[-1], start = 1992, frequency = 1) ; plot(DF.TS) ; plot(DF.TS, plot.type="single", col = 1:ncol(DF.TS)) ; legend("bottomleft", colnames(DF.TS), col=1:3, lty=1, cex=.65) – Roamaa May 31 '15 at 08:17
  • 1
    Thanks alot for your help akrun. I really appreciate it. And if the repos=http://cran.rstudio.com doesnt work. I shall ask here. Thank you so much – Roamaa May 31 '15 at 08:18

1 Answers1

0

After we merge, create a 'xts' object ordered by the date (which is created from the 'year' column), and then use plot.xts

 library(xts)
 library(xtsExtra) 
 df1 <- merge(j5, c4, left_on='year', how='inner')
 xt1 <- xts(df1[-1], order.by=as.Date(paste0(df1$year,'-01-01')))
 plot.xts(xt1)

data

df1 <- structure(list(year = 1992:2014, tradevaluejapan = c(1215216L, 
654958L, 1058199L, 4827964L, 2882890L, 5859965L, 3499868L, 3530975L, 
439580L, 9808706L, 9358647L, 1348892L, 945569L, 738876L, 1453525L, 
9335089L, 5391225L, 55391L, 1246401L, 25783L, 88171L, 329110L, 
1066022L), tradevaluechina = c(265860L, 300987L, 693368L, 1608935L, 
1457594L, 880888L, 859137L, 1069889L, 2026727L, 2675847L, 4568524L, 
4608139L, 6619693L, 14812109L, 17870811L, 19904723L, 26881686L, 
18815007L, 20065842L, 19004412L, 17278793L, 19116949L, 32076571L
)), .Names = c("year", "tradevaluejapan", "tradevaluechina"), 
class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23"))
akrun
  • 874,273
  • 37
  • 540
  • 662