A marginal plot is a scatterplot (sometimes a density plot or other bivariate plot) that has histograms, boxplots, or other distribution visualization layers in the margins of the x- and y-axes.
It allows studying the relationship between 2 numeric variables.
You can use the ggmarginal()
function to add marginal layers to a plot.
import numpy as np
import pandas as pd
from lets_plot import *
from lets_plot.mapping import as_discrete
LetsPlot.setup_html()
# The default plot theme and palette.
LetsPlot.set_theme(theme_light())
palette = scale_color_manual(values=["#394449", "#F7C443"]) + scale_fill_manual(values=["#394449", "#F7C443"])
np.random.seed(0)
cov0=[[1, -.8],
[-.8, 1]]
cov1=[[ 10, .1],
[.1, .1]]
x0, y0 = np.random.multivariate_normal(mean=[-2,0], cov=cov0, size=200).T
x1, y1 = np.random.multivariate_normal(mean=[0,1], cov=cov1, size=200).T
data = dict(
x = np.concatenate((x0,x1)),
y = np.concatenate((y0,y1)),
c = ["A"]*200 + ["B"]*200
)
p = ggplot(data, aes("x", "y", color="c", fill="c")) + geom_point() + palette
p
# Add marginal layers to any side on the plot:
# - [l]eft
# - [r]ight
# - [t]op
# - [b]ottom
p + ggmarginal("tr", layer=geom_histogram(color="white")) \
+ ggmarginal("lb", layer=geom_density(size=0))
# Set constant colors to disable the grouping.
p_hist = (p
+ ggmarginal("tr", layer=geom_histogram(fill="gray", color="white"))
+ ggmarginal("tr", layer=geom_density(fill="rgba(0,0,0,0)", color="red")))
p_hist
# Add marginal boxplots.
# Make margins smaller using the parameter `size`.
p_hist + ggmarginal("lb", size=0.03, layer=geom_boxplot(fill="white", color="black"))
# Combine marginal violin and boxplot.
# Make margins much larger.
p + ggmarginal("rb", size=0.4, layer=geom_violin(trim=False, color="black")) \
+ ggmarginal("rb", layer=geom_boxplot(aes(group="c"), fill="white", color="white",
alpha=.25, outlier_color="red", width=.2))
# Show marginal densities on a 2D-density plot.
ggplot(data, aes("x", "y")) + geom_density2df(aes(fill="..level..")) + coord_cartesian() \
+ ggmarginal("tr", layer=geom_area(stat="density"))