-2

I am writing a Shiny App based on Cointegration.

After I find the cointegration vectors I plot all the mean reverting series using ggplot2. The way I do it is I put all these cointegration series in a dataframe and I melt it then I facet wrap based on the variable.

Until here I had no problem.

enter image description here

Now I want to add the moving average and the moving standard deviation to the plot of each cointegration series but I can't find a way to have each series with it's MA and STD alone.

I know the problem with what I have right now is with the way I am facet_wraping but I am not an experienced user so I do not know how to solve it. Now I have:

enter image description here

I want the Cointegration Series 1, MA series1 and Std Series 1 to be on the same graph and have different colours and the same for Cointegration Series 2.... The code I use is:

 m= melt(DF, id.vars = "Date") # melt the df

ggplot(m, aes(x = Date, y = value, colour = variable, group=variable)) +geom_line() + facet_wrap( ~ variable) # plot

Is there a way to wrap the first 3 columns together ? Should I melt in a different way ?? Do I need to explain more to be more clear ?

EDIT: Basically in Lehman's terms suppose I have a dataframe df composed of the columns:

Date, S1, MA1, STD1, S2, MA2, STD2

I want my x axis to be Date, and I want to have S1, MA1, STD1 on a plot and S2, MA2 and STD2 on another plot using ggplot2. As id I were facet wraping S1,MA1, STD1 together and then S2, MA2, STD2 also together

Suppose this is the DataFrame:

        Date           S1           M1          ST1         S2          M2        ST2
1 02/12/1999 -0.000217052  0.002862195 -0.002390842  2.2025825 -1.17258213 -0.3057015
2 03/12/1999 -0.004882038 -0.015920939 -0.007014382 -0.4040079 -0.69496488  0.1000167
3 06/12/1999 -0.001445954  0.005077610  0.000000000  1.1573779 -1.64268166 -0.3847015
4 07/12/1999 -0.000907952 -0.005403168  0.002027728 -0.3165827 -1.16466940 -1.0113501
5 08/12/1999  0.000881220  0.011375226 -0.013524439  0.5154455 -0.51725208  1.0942177
6 09/12/1999  0.001043752  0.013400502  0.017033342  1.9368608 -0.05587143  0.5203337

Is it clear enough now ?

J.Doe
  • 166
  • 1
  • 9
  • 1
    Please learn about [reproducible questions](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) (another ref: https://stackoverflow.com/help/mcve), it will make things significantly easier. I suggest (at a minimum): remove non-essential code references (such as shiny), and provide *sample* data for a simplified problem. – r2evans Dec 30 '17 at 16:13
  • See edit I think it will make it clearer – J.Doe Dec 30 '17 at 16:25
  • Welcome to SO. We don't have your data. You don't need to show us a Shiny fragment for this question. We just need minimal sample data and the ggplot code that's representative and reproducible to help you. As it stands, your question is likely to get closed. – hrbrmstr Dec 30 '17 at 16:58
  • I edited it again please help !! – J.Doe Dec 30 '17 at 18:21

1 Answers1

0

I want my x axis to be Date, and I want to have S1, MA1, STD1 on a plot and S2, MA2 and STD2 on another plot using ggplot2.

If you reshape your data as follows:

         Date  S variable        value
1  02/12/1999 S1    value -0.000217052
2  03/12/1999 S1    value -0.004882038
3  06/12/1999 S1    value -0.001445954
4  07/12/1999 S1    value -0.000907952
5  08/12/1999 S1    value  0.000881220
6  09/12/1999 S1    value  0.001043752
7  02/12/1999 S2    value  2.202582500
8  03/12/1999 S2    value -0.404007900
9  06/12/1999 S2    value  1.157377900
10 07/12/1999 S2    value -0.316582700
11 08/12/1999 S2    value  0.515445500
12 09/12/1999 S2    value  1.936860800
13 02/12/1999 S1     mavg  0.002862195
14 03/12/1999 S1     mavg -0.015920939
15 06/12/1999 S1     mavg  0.005077610
16 07/12/1999 S1     mavg -0.005403168
17 08/12/1999 S1     mavg  0.011375226
18 09/12/1999 S1     mavg  0.013400502
19 02/12/1999 S2     mavg -1.172582130
20 03/12/1999 S2     mavg -0.694964880
21 06/12/1999 S2     mavg -1.642681660
22 07/12/1999 S2     mavg -1.164669400
23 08/12/1999 S2     mavg -0.517252080
24 09/12/1999 S2     mavg -0.055871430
25 02/12/1999 S1   stddev -0.002390842
26 03/12/1999 S1   stddev -0.007014382
27 06/12/1999 S1   stddev  0.000000000
28 07/12/1999 S1   stddev  0.002027728
29 08/12/1999 S1   stddev -0.013524439
30 09/12/1999 S1   stddev  0.017033342
31 02/12/1999 S2   stddev -0.305701500
32 03/12/1999 S2   stddev  0.100016700
33 06/12/1999 S2   stddev -0.384701500
34 07/12/1999 S2   stddev -1.011350100
35 08/12/1999 S2   stddev  1.094217700
36 09/12/1999 S2   stddev  0.520333700

Then you can get the plot you want as follows:

ggplot(df, aes(x=Date)) + geom_line(aes(y=value, group=variable, colour=variable)) + facet_wrap(~ S)

Which results in:

enter image description here

Reshaping the data:

There are various packages available for reshaping data including tidyr, reshape2, etc. I'm still learning myself how to use these but this is the convoluted way I reshaped it. I'm sure there are folks that could do this more efficiently. :)

# read the original data
df <- read.table("/tmp/table.dat")

# separate s1 and s2
df.s1 <- df[,1:4]
df.s2 <- df[,c(1,5:7)]

# convert s1 and s2 to long form
df.s1.melted <- melt(df.s1, id.vars=c("Date","M1","ST1"))
df.s2.melted <- melt(df.s2, id.vars=c("Date","M2","ST2"))

# rename columns of s1 and s2 so they can be combined with rbind
s1 <- setNames(df.s1.melted, c("Date", "mavg", "stddev", "S", "value"))
s2 <- setNames(df.s2.melted, c("Date", "mavg", "stddev", "S", "value"))

# combine s1 and s2 with rbind then convert mavg, stddev and value to long form
df <- melt(rbind(s1, s2), id.vars=c("Date", "S"))

# plot it
ggplot(df, aes(x=Date)) + geom_line(aes(y=value, group=variable, colour=variable)) + facet_wrap(~ S)
Mike N.
  • 1,662
  • 1
  • 13
  • 19
  • can you please tell me how to reshape my data to have this. Sorry I am really an extreme beginner ! Thank you in advance sir – J.Doe Dec 30 '17 at 19:22