facets
.¶You want to see more aspects of your data and it's not practcal 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.
To learn more about formatting templates see: Formatting.
%useLatestDescriptors
%use lets-plot
%use dataframe
LetsPlot.getInfo()
Lets-Plot Kotlin API v.4.4.1. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.3.2.0.
var data = DataFrame.readCSV("https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg2.csv")
data.head(3)
DataFrame: rowsCount = 3, columnsCount = 9
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.
val p = letsPlot(data.toMap()) {x="engine horsepower"; y="miles per gallon"} +
geomPoint {color="origin of car"} +
themeGrey()
p + ggsize(800, 350)
There are two functions for faceting:
The former creates 2-D matrix of plot panels and latter creates 1-D strip of plot panels.
We'll be using the number of cylinders
variable as 1st fatceting variable, and sometimes the origin of car
as a 2nd fatceting 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 + facetGrid(x="number of cylinders")
Split up the data by two faceting variables: number of cylinders
and origin of car
.
p + facetGrid(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 + facetGrid(x="number of cylinders", y="origin of car", xFormat="{d} cyl", yOrder=-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 + facetWrap(facets="number of cylinders", nrow=2)
Split data by origin of car
and number of cylinders
and arrange tiles in 5 columns.
p + facetWrap(facets=listOf("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 + facetWrap(facets=listOf("origin of car", "number of cylinders"),
ncol=3,
format=listOf(null, "{} cyl"),
order=listOf(1, -1),
dir="v")