facets
.¶You want to see more aspects of your data and it's not practical to use the regular aesthetics
approach for that.
facets
¶You can add one or more new dimentions to your plot using faceting
.
This approach allows you to split up your data by one or more variables and plot the subsets of data together.
In this demo we will explore how various faceting functions work, as well as the built-in sorting
and formatting
options.
import numpy as np
import pandas as pd
from lets_plot import *
LetsPlot.setup_html()
data = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg2.csv')
data.head(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 |
Create a scatter plot to show how mpg
is related to a car's engine horsepower
.
Also use the color
aesthetic to vizualise the region where a car was designed.
p = (ggplot(data, aes(x="engine horsepower", y="miles per gallon")) +
geom_point(aes(color="origin of car")))
p + ggsize(800, 350)
There are two functions for faceting:
The first one creates a 2D matrix of plot panels and the second creates a one dimensional strip of plot panels.
We will use the number of cylinders
variable as 1st faceting variable, and sometimes the origin of car
as a 2nd faceting variable.
The data can be split up by one or two variables that vary on the X and/or Y direction.
Let's split up the data by number of cylinders
.
p + facet_grid(x="number of cylinders")
Split up the data by two faceting variables: number of cylinders
and origin of car
.
p + facet_grid(x="number of cylinders", y="origin of car")
Apply a formatting template to the number of cylinders
and
sort the origin of car
values in discending order.
To learn more about formatting templates see: Formatting.
p + facet_grid(x="number of cylinders", y="origin of car", x_format="{d} cyl", y_order=-1)
The data can be split up by one or more variables.
The panels layout is flexible and controlled by ncol
, nrow
and dir
options.
Split data by the number of cylinders
variable and arrange tiles in two rows.
p + facet_wrap(facets="number of cylinders", nrow=2)
Split data by origin of car
and number of cylinders
and arrange tiles in 5 columns.
p + facet_wrap(facets=["origin of car", "number of cylinders"], ncol=5)
Use the dir
parameter to arrange tiles by columns, in 3 columns (the default tile arrangment is "by row").
Also, format number of cylinders
labels and reverse the sorting direction for this facetting variable.
p + facet_wrap(facets=["origin of car", "number of cylinders"],
ncol=3,
format=[None, "{} cyl"],
order=[1, -1],
dir="v")