Search code examples
rplotfacet-grid

plotting multiple charts using facet grid


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


Solution

  • 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).