Statistics, Modeling & Visualization · Models, inference, and charts
phiz
A ggplot2-flavored grammar of graphics for PHP that emits Vega-Lite.
composer require phpstats/phiz
- Optional
- vl-convert (SVG, PNG, and PDF export) Laravel (the <x-phiz> Blade component and the @phizScripts directive) phit (predictions from formula fits in effect_plot(), predictions(), and marginal_means())
- Platforms
- Linux macOS
phiz describes a chart the way ggplot2 does: data, aesthetic mappings, layers of geoms, scales, coordinates, facets, labels, and themes. It builds a Vega-Lite specification in PHP and the browser draws it, so there is no JavaScript to write.
Plots take rows, columns, a phibble table, a Laravel collection, or a URL that Vega-Lite fetches itself. Dozens of geoms cover points, lines, areas, bars, distributions, intervals, heatmaps, densities, contours, and model fits. Scales, facets, label formatters, and guides keep ggplot2 names and arguments, and the theme presets follow its complete themes.
Interaction is expressed as Vega-Lite parameters, so tooltips, panning, zooming, brushing, and linked highlighting work in any page that renders the specification. The <x-phiz> Blade component draws a plot and redraws it when a Livewire component updates. The vl-convert binary turns the same specification into SVG, PNG, or PDF for reports and email.
The statistics helpers turn model results into ordinary plots: coefficient plots, regression diagnostics, predicted effects, marginal means, and random-effect intervals. Any plot method can restyle them.
Example
use function Phiz\{aes, facet_wrap, geom_point, ggplot, label_dollar, labs, scale_y_continuous};
$orders = \Phiz\Data::url('/exports/orders.csv');
$plot = ggplot($orders, aes(x: 'items:Q', y: 'total:Q', color: 'channel:N')) |>
geom_point(alpha: 0.6) |>
facet_wrap('region', ncol: 3) |>
scale_y_continuous(labels: label_dollar()) |>
labs(x: 'Items', y: 'Order total', color: 'Channel');
echo $plot->toJson(pretty: true);
use function Phiz\label_dollar;
$orders = \Phiz\Data::url('/exports/orders.csv');
$plot = \Phiz\Plot::of($orders)
->aes(x: 'items:Q', y: 'total:Q', color: 'channel:N')
->point(alpha: 0.6)
->facetWrap('region', ncol: 3)
->scaleY(labels: label_dollar())
->labs(x: 'Items', y: 'Order total', color: 'Channel');
echo $plot->toJson(pretty: true);
Output
{
"$schema": "https://vega.github.io/schema/vega-lite/v6.json",
"data": {
"url": "/exports/orders.csv"
},
"facet": {
"field": "region",
"type": "nominal",
"title": null
},
"columns": 3,
"spec": {
"mark": {
"type": "point",
"filled": true,
"opacity": 0.6
},
"encoding": {
"x": {
"field": "items",
"type": "quantitative",
"title": "Items",
"scale": {
"zero": false
}
},
"y": {
"field": "total",
"type": "quantitative",
"title": "Order total",
"axis": {
"labelExpr": "\"$\" + format(datum.value, ',~f')"
},
"scale": {
"zero": false
}
},
"color": {
"field": "channel",
"type": "nominal",
"title": "Channel"
}
}
},
"config": {
"legend": {
"labelLimit": 320
}
}
}