In [1]:
%matplotlib inline
from ggplot import *

Building Faceted Plots

Facets allow you to visualize high-dimensional data quickly and (most of the time) easily. Faceting splits your data up into differnet subgroups based upon different variables.

We're going to be using the diamonds dataset in these examples. It might be helpful to refresh yourself with the dataset before jumping into the samples.


In [2]:
diamonds.head()


Out[2]:
carat cut color clarity depth table price x y z
0 0.23 Ideal E SI2 61.5 55.0 326 3.95 3.98 2.43
1 0.21 Premium E SI1 59.8 61.0 326 3.89 3.84 2.31
2 0.23 Good E VS1 56.9 65.0 327 4.05 4.07 2.31
3 0.29 Premium I VS2 62.4 58.0 334 4.20 4.23 2.63
4 0.31 Good J SI2 63.3 58.0 335 4.34 4.35 2.75

In [3]:
ggplot(diamonds, aes(x='carat', y='price')) + geom_point() + facet_grid('clarity')


Out[3]:
<ggplot: (285190993)>

In [4]:
ggplot(diamonds, aes(x='carat', y='price')) + geom_point() + facet_grid(None, 'clarity')


Out[4]:
<ggplot: (290016845)>

In [5]:
ggplot(diamonds, aes(x='carat', y='price')) + geom_point() + facet_wrap('clarity')


Out[5]:
<ggplot: (290937069)>

In [6]:
ggplot(diamonds, aes(x='carat', y='price')) + geom_point() + facet_wrap('clarity', ncol=4)


Out[6]:
<ggplot: (290596545)>

In [7]:
ggplot(diamonds, aes(x='carat', y='price')) + geom_point() + facet_wrap('clarity', 'cut')


Out[7]:
<ggplot: (292466405)>

In [8]:
ggplot(diamonds, aes(x='carat', y='price', shape='cut', color='clarity')) + \
    geom_point() + \
    scale_color_brewer(type='qual') + \
    facet_grid('color')


Out[8]:
<ggplot: (299533301)>

In [9]:
ggplot(diamonds, aes(x='carat', y='price', color='depth')) + \
    geom_point() + \
    scale_color_gradient(low='white', high='black') + \
    facet_grid('color', 'clarity')


Out[9]:
<ggplot: (300301273)>

In [ ]: