Facets and grids¶
Small multiples with facet_wrap¶
facet_wrap draws one panel per value of a column. The panels share their scales, so
they are easy to compare.
import polars as pl
from ggrust import *
DATA = "https://raw.githubusercontent.com/datavil/ggrust/main/docs/data/"
homes = pl.read_csv(DATA + "homes.csv")
plot = (
ggplot(homes, aes("area_m2", "price_k"))
+ geom_point(aes(color="year_built"), size=1.8, alpha=0.8)
+ facet_wrap("neighbourhood", ncol=2)
+ scale_color_viridis(format="d")
+ labs(x="Floor area (m²)", y="Price (thousands)", color="Built")
+ theme_bw()
+ ggsize(640, 520)
)
plot
ncol and nrow set the layout. Each panel can have its own axis range with
scales="free", "free_x" or "free_y".
Facets combine with any layer. Here each city gets a ribbon from the daily low to the daily high:
import polars as pl
from ggrust import *
DATA = "https://raw.githubusercontent.com/datavil/ggrust/main/docs/data/"
cities = pl.read_csv(DATA + "cities.csv")
plot = (
ggplot(cities, aes("month"))
+ geom_ribbon(aes(ymin="low", ymax="high", fill="city"), alpha=0.3, color="rgba(0,0,0,0)")
+ geom_line(aes(y="temperature", color="city"), size=1)
+ facet_wrap("city", ncol=5)
+ scale_x_continuous(breaks=[1, 4, 7, 10], labels=["Jan", "Apr", "Jul", "Oct"])
+ labs(x="", y="Temperature (°C)")
+ theme_bw()
+ theme(legend_position="none")
+ ggsize(760, 300)
)
plot
Several plots with gggrid¶
gggrid arranges different plots in one figure. Pass a list of plots and ncol;
widths and heights set relative sizes, and align=True lines up their panels.
import polars as pl
from ggrust import *
DATA = "https://raw.githubusercontent.com/datavil/ggrust/main/docs/data/"
homes = pl.read_csv(DATA + "homes.csv")
coffee = pl.read_csv(DATA + "coffee.csv")
cities = pl.read_csv(DATA + "cities.csv")
prices = (
ggplot(homes, aes("neighbourhood", "price_k", fill="neighbourhood"))
+ geom_violin(alpha=0.6)
+ labs(x="", y="Price (thousands)")
+ theme(legend_position="none")
)
drinks = (
ggplot(coffee, aes("drink", "cups"))
+ geom_bar(stat="identity", fill="#6f4e37")
+ coord_flip()
+ labs(x="", y="Cups per week")
)
weather = (
ggplot(cities, aes("month", "temperature", color="city"))
+ geom_line(size=1)
+ labs(x="Month", y="°C", color="")
)
plot = gggrid([prices, drinks, weather], ncol=3) + ggsize(1000, 320)
plot