Handling of Empty Panels in facet_wrap()

In [1]:
import pandas as pd
from lets_plot import *
In [2]:
LetsPlot.setup_html()
In [3]:
df = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg2.csv')
df.head(3)
Out[3]:
miles per gallon number of cylinders engine displacement (cu. inches) engine horsepower vehicle weight (lbs.) time to accelerate (sec.) model year origin of car vehicle name
0 18.0 8 307.0 130 3504 12.0 70 US chevrolet chevelle malibu
1 15.0 8 350.0 165 3693 11.5 70 US buick skylark 320
2 18.0 8 318.0 150 3436 11.0 70 US plymouth satellite
In [4]:
p = (
  ggplot(df, 
         aes('engine horsepower', 'miles per gallon', 
             color=as_discrete('origin of car',order=-1)))
  + geom_point()
  + theme_grey() + ggtitle('Efficiency vs Engine Horsepower')
)
p
Out[4]:

Empty Facet Panels are Dropped by Default

For example, there are no 3-cylinder engine cars originated in the US. Accordingly, this facet panel is dropped.

In [5]:
p + facet_wrap(['origin of car', 'number of cylinders'], order=[-1, 1], ncol=3)
Out[5]:

Using the drop Parameter to Control Empty Panels

Use drop=False to show all factor levels regardless of whether they occur in the data.

In [6]:
p + facet_wrap(['origin of car', 'number of cylinders'], order=[-1,1], ncol=5,
               drop=False
)
Out[6]: