Residual plot

In [1]:
import pandas as pd

from lets_plot import *
from lets_plot.bistro.residual import *
LetsPlot.setup_html()
In [2]:
df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/iris.csv")
print(df.shape)
df.head()
(150, 5)
Out[2]:
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa

Default plot

In [3]:
residual_plot(df, "petal_length", "petal_width")
Out[3]:

Scatter plot (method='none')

In [4]:
residual_plot(df, "petal_length", "petal_width", method='none', hline=False, marginal='none') + theme_classic()
Out[4]:

Change geom type

In [5]:
residual_plot(df, "petal_length", "petal_width", geom='tile', marginal="hist:tr") + \
    ggsize(600, 200)
Out[5]:

Change method

In [6]:
residual_plot(df, "petal_length", "petal_width", method='loess', span=.25, max_n=25)
Out[6]:

Geometries customization

In [7]:
residual_plot(df, "petal_length", "petal_width", size=5, color="#feb24c", alpha=1/3)
Out[7]:
In [8]:
residual_plot(df, "petal_length", "petal_width", geom='none', color="black", hline=False) + \
    geom_hline(yintercept=0, color="black") + \
    geom_point(size=5, shape=21, color="black", fill="#feb24c")
Out[8]:
In [9]:
residual_plot(df, "petal_length", "petal_width", hline=False, marginal='none') + \
    geom_smooth(method='loess', se=True, level=.99, seed=42)
Out[9]:

Marginal layers customization

In [10]:
residual_plot(df, "petal_length", "petal_width", marginal="box:lb:.03,hist:t:.4,hist:r", color="black") + \
    ggmarginal("tr", layer=geom_area(stat='density', color="magenta", fill="magenta", alpha=.1)) + \
    theme_minimal()
Out[10]:

Grouping

In [11]:
residual_plot(df, "petal_length", "petal_width", color_by="species")
Out[11]:

Interaction with other layers

In [12]:
residual_plot(df, "petal_length", "petal_width", geom='tile', binwidth=[.4, .1], marginal='none') + \
    coord_fixed(ratio=.25, flip=True) + \
    theme(axis_ticks="blank", axis_text="blank", axis_line="blank")
Out[12]:
In [13]:
residual_plot(df, "petal_length", "petal_width", color="white", binwidth=.1, marginal="hist:r") + \
    ylab("residual") + \
    theme_bw() + theme(text=element_text(family="monospace")) + \
    flavor_high_contrast_dark()
Out[13]: