ggplot multiple plots in one object
<p>I've created a script to create multiple plots in one object. The results I am looking for are two plots one over the other such that each plot has different y axis scale but x axis is fixed - dates. However, only one of the plots (the top) is properly created, the bottom plot is visible but empty i.e the <code>geom_line</code> is not visible. Furthermore, the y-axis of the second plot does not match the range of values - min to max. I also tried using <code>facet_grid (scales="free")</code> but no change in the y-axis. The y-axis for the second graph has a range of 0 to 0.05.</p>

<p>I've limited the date range to the past few weeks. This is the code I am using:</p>

<pre><code>    df = df.set_index('date')
    weekly = df.resample('w-mon',label='left',closed='left').sum()
    data = weekly[-4:].reset_index()
    data= pd.melt(data, id_vars=['date'])
    pplot = ggplot(aes(x="date", y="value", color="variable", group="variable"), data)
    #geom_line()
    scale_x_date(labels = date_format('%d.%m'), 
                         limits=(data.date.min() - dt.timedelta(2),
                                 data.date.max() + dt.timedelta(2)))
    #facet_grid("variable", scales="free_y")
    theme_bw()
</code></pre>

<p>The dataframe sample (df), its a daily dataset containing values for each variable x and a, in this case 'date' is the index:</p>

<pre><code>date            x      a
2016-08-01      100    20
2016-08-02       50    0
2016-08-03       24    18
2016-08-04       0     10
</code></pre>

<p>The dataframe sample (to_plot) - weekly overview:</p>

<pre><code>         date       variable  value
0  2016-08-01       x        200
1  2016-08-08       x        211
2  2016-08-15       x        104
3  2016-08-22       x        332
4  2016-08-01       a         8
5  2016-08-08       a         15
6  2016-08-15        a        22
7  2016-08-22        a        6
</code></pre>

<p>Sorry for not adding the df dataframe before.</p>

<p>Your calls to the plot directives <code>geom_line()</code>, <code>scale_x_date()</code>, etc. are standing on their own in your script; you do not connect them to your plot object. Thus, they do not have any effect on your plot.</p>

<p>In order to apply a plot directive to an existing plot object, use the <em>graphics language</em> and "add" them to your plot object by connecting them with a <code>+</code> operator.</p>

<p>The result (as intended):</p>

<p><a href="http://i.stack.imgur.com/Pz1Ky.png" rel="nofollow"><img src="http://i.stack.imgur.com/Pz1Ky.png" alt="ggplot in python with facet_grid"></a></p>

<p>The full script:</p>

<pre><code>from __future__ import print_function

import sys
import pandas as pd
import datetime as dt
from ggplot import *

if __name__ == '__main__':
    df = pd.DataFrame({
        'date': ['2016-08-01', '2016-08-08', '2016-08-15', '2016-08-22'],
        'x': [100, 50, 24, 0],
        'a': [20, 0, 18, 10]
    })

    df['date'] = pd.to_datetime(df['date'])

    data = pd.melt(df, id_vars=['date'])
    plt = ggplot(data, aes(x='date', y='value', color='variable', group='variable')) +\
        scale_x_date(
            labels=date_format('%y-%m-%d'), 
            limits=(data.date.min() - dt.timedelta(2), data.date.max() + dt.timedelta(2))
        ) +\
        geom_line() +\
        facet_grid('variable', scales='free_y')

    plt.show()
</code></pre>

python plot python-ggplot