Missing Values: Line, Path, Area, and Ribbon

Missing value (NaN or None) handling in geom_line(), geom_path(), geom_area(), and geom_ribbon().

Note: geom_path() handles missing values slightly differently than the others.

In [1]:
import numpy as np
import pandas as pd
from datetime import datetime
from lets_plot import *
In [2]:
LetsPlot.setup_html()
In [3]:
economics_url = 'https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/economics.csv'
economics = pd.read_csv(economics_url)
economics['date'] = pd.to_datetime(economics['date'])
start = datetime(2006, 1, 1)
economics = economics.loc[economics['date'] >= start]
In [4]:
economics.head()
Out[4]:
Unnamed: 0 date pce pop psavert uempmed unemploy
462 463 2006-01-01 9059.8 297647.0 4.2 8.6 7064
463 464 2006-02-01 9090.1 297854.0 4.2 9.1 7184
464 465 2006-03-01 9122.1 298060.0 4.2 8.7 7072
465 466 2006-04-01 9174.8 298281.0 4.0 8.4 7120
466 467 2006-05-01 9215.1 298496.0 3.8 8.5 6980
In [5]:
# Make some gaps in the data
economics_with_gaps = economics.copy()
economics_with_gaps.loc[
    (economics_with_gaps['date'] >= '2008-01-01') & 
    (economics_with_gaps['date'] <= '2009-06-01'), 
    'date'
] = None
economics_with_gaps.loc[
    (economics_with_gaps['date'] >= '2012-01-01') & 
    (economics_with_gaps['date'] <= '2012-04-01'), 
    'unemploy'
] = np.nan

1. Line

In [6]:
( 
  ggplot(mapping=aes('date', 'unemploy')) + 
    geom_line(data=economics, size=10, alpha=0.1, tooltips='none') + 
    geom_line(data=economics_with_gaps, color='teal') + 
    geom_label(label='Missing dates', x=datetime(2008, 8, 15), y=11000, nudge_x=-70, nudge_unit='px') +
    geom_label(label='Missing unemployment\nfigures', x=datetime(2012, 2, 15), y=13000, nudge_x=80, nudge_y=40, nudge_unit='px') +
    theme_classic() + 
    ggsize(800, 300)
) 
Out[6]:

2. Path

In [8]:
( 
  ggplot(mapping=aes('date', 'unemploy')) + 
    geom_line(data=economics, size=10, alpha=0.1, tooltips='none') + 
    geom_path(data=economics_with_gaps, color='teal') + 
    geom_label(label='Missing dates', x=datetime(2008, 8, 15), y=11000, nudge_x=-70, nudge_unit='px') +
    geom_label(label='Missing unemployment\nfigures', x=datetime(2012, 2, 15), y=13000, nudge_x=80, nudge_y=40, nudge_unit='px') +
    theme_classic() + 
    ggsize(800, 300)
) 
Out[8]:

3. Area

In [9]:
( 
  ggplot(mapping=aes('date', 'unemploy')) + 
    geom_line(data=economics, size=10, alpha=0.1, tooltips='none') + 
    geom_area(data=economics_with_gaps, color='teal', fill='teal', alpha=0.2) + 
    geom_label(label='Missing dates', x=datetime(2008, 8, 15), y=12000, nudge_x=-70, nudge_unit='px') +
    geom_label(label='Missing unemployment\nfigures', x=datetime(2012, 2, 15), y=13000, nudge_x=80, nudge_y=40, nudge_unit='px') +
    theme_classic() + 
    coord_cartesian(ylim=[None, 20000]) +
    ggsize(800, 300)
) 
Out[9]:

4. Ribbon

In [10]:
# Make some gaps in the data
economics_with_gaps2 = economics.copy()
economics_with_gaps2.loc[
    (economics_with_gaps2['date'] >= '2008-01-01') & 
    (economics_with_gaps2['date'] <= '2009-06-01'), 
    'date'
] = None
economics_with_gaps2.loc[
    (economics_with_gaps2['date'] >= '2011-08-01') & 
    (economics_with_gaps2['date'] <= '2011-11-01'), 
    'uempmed'
] = np.nan
economics_with_gaps2.loc[
    (economics_with_gaps2['date'] >= '2013-05-01') & 
    (economics_with_gaps2['date'] <= '2013-08-01'), 
    'psavert'
] = np.nan
In [11]:
( 
  ggplot(mapping=aes('date', ymin='psavert', ymax='uempmed')) + 
    geom_line(aes(y='psavert'), data=economics, size=10, alpha=0.1, tooltips='none') + 
    geom_line(aes(y='uempmed'), data=economics, size=10, alpha=0.1, tooltips='none') + 
    geom_ribbon(data=economics_with_gaps2, color='teal', fill='teal', alpha=0.2) + 
    geom_label(label='Missing dates', x=datetime(2008, 8, 15), y=13, nudge_x=-70, nudge_unit='px') +
    geom_label(label='Missing unemployment figures', x=datetime(2011, 8, 15), y=24, nudge_x=80, nudge_unit='px') +
    geom_label(label='Missing savings rate figures', x=datetime(2013, 5, 15), y=3.5, nudge_x=50, nudge_unit='px') +
    theme_classic() + 
    ggsize(800, 300)
) 
Out[11]: