0

I'm working on a time series of different stocks, and I am getting some issues in plotting them efficiently.

So my dataset looks like this:

              A       B       C       D
1/2/2012    0.007   0.012   0.015   0.009
1/3/2012    0.009   0.012   0.015   0.008
1/4/2012    0.012   0.012   0.015   0.009
1/5/2012    0.013   0.012   0.015   0.012
1/6/2012    0.013   0.012   0.015   0.011
1/9/2012    0.013   0.012   0.015   0.011
1/10/2012   0.013   0.009   0.015   0.011
1/11/2012   0.013   0.009   0.015   0.014
1/12/2012   0.013   0.009   0.015   0.014
1/13/2012   0.013   0.009   0.015   0.013
1/16/2012   0.013   0.012   0.014   0.017
1/17/2012   0.013   0.013   0.015   0.017
1/18/2012   0.014   0.013   0.015   0.018
1/19/2012   0.014   0.013   0.015   0.018
1/20/2012   0.015   0.012   0.015   0.018
1/24/2012   0.016   0.011   0.016   0.018
1/25/2012   0.016   0.011   0.016   0.019
1/26/2012   0.016   0.010   0.015   0.021
1/27/2012   0.016   0.010   0.015   0.022
1/30/2012   0.016   0.010   0.015   0.022
1/31/2012   0.016   0.010   0.015   0.022
2/1/2012    0.016   0.010   0.015   0.022
2/2/2012    0.020   0.012   0.015   0.025

Right now, I plotted all of the manually gX <- ggplot(dat, aes(Index, X)) + geom_line and plotted them together through grid.arrange(g1, g2, g3, g4)

Is there a way to plot these using facet_grid since the way I did it was inefficient and rigid?

Below is the plot I did manually:

enter image description here

samael
  • 29
  • 1
  • 6
  • What are the `Index` and `X` column – akrun Sep 22 '17 at 07:41
  • Have you looked at the documentation for facet_grid? You simply need a grouping variable for each of your data 'subsets'; I'm guessing in your case this is things like AC and AEV? Then you just call `facet_grid(~groupingVariable)`. – Quinn Sep 22 '17 at 07:41
  • `facet_wrap` would be a better choice, as there appears to be only a single grouping variable: `facet_wrap(~stock, ncol=5, scales='free_y')` could produce something similar, but the name of the stock would be on top of each panel. – MrGumble Sep 22 '17 at 08:43

1 Answers1

3

Assuming in your data.frame A, B, C, and D are stock names, you must first create a long-table of the following form:

Date       Stock   value
1/2/2012   A       0.007
1/2/2012   B       0.012
1/2/2012   C       0.015
1/2/2012   D       0.009
1/3/2012   A       0.009
1/3/2012   A       0.012
1/3/2012   A       0.015
1/3/2012   A       0.008

I'll assume your first column is rownames, and that will have to be imported as a column into your data frame. Then use gather from tidyr to create the data frame in long-format.

library(tidyr)
df$Date <- rownames(df)
long <- gather(df, Stock, value, -Date)
ggplot(aes(x=Date, y=Value)) + geom_line() + facet_wrap(~Stock, scales='free_y', ncol=5)

In gather, first argument the 2nd argument is the name of the column that will contain the earlier column names, and the 3rd argument is the name of the column that will contain their values. The following arguments to gather are to select which columns to gather, in this case it is all except Date (since you have many and wouldn't want to bother typing them all manually).

MrGumble
  • 5,631
  • 1
  • 18
  • 33
  • Thanks MrGumble, I wasn't aware that I have to convert my data into long-table first to use facets! – samael Sep 23 '17 at 00:20
  • You're welcome. For ggplot2, the starting point is generally to have your data in long-table format: 1 column, 1 variable. All x-values in the same column, all y-axes in the same column, all variables for colour in the same column, etc. Of course, at some point, you'll have to deviate from this. – MrGumble Sep 25 '17 at 07:38
  • trelliscopejs is a great package for displaying multiple plots with interactivity, the ability to filter and sort once produced as a whole. https://hafen.github.io/trelliscopejs/articles/trelliscopejs.html – Melissa Salazar Jan 28 '21 at 21:34