phiz

Guides

Themes

theme() takes a preset and individual options. The presets follow ggplot2’s complete themes:

Preset Look
vega Vega-Lite’s defaults
gray (also grey, ggplot2) gray panel with white grid lines, and ggplot2’s hue palette
bw white panel with a dark border and light grid
linedraw white panel with a black border and thin black grid lines
minimal light grid, no border, and no axis lines
classic axis lines and no grid
light light gray border and grid
dark dark background
void no axes or grid

baseSize: scales every font size, with ggplot2’s proportions. Themes are written into the specification as Vega-Lite config, so HTML and static exports look the same.

use Phiz\Datasets;
use Phiz\Plot;

$plot = Plot::of(Datasets::mpg())
    ->aes(x: 'displ', y: 'hwy', color: 'drv')
    ->point()
    ->theme('gray', baseSize: 12);

The individual options are:

  • font, textColor, and baseSize
  • legendPosition (right, left, top, bottom, or none), legendDirection (horizontal or vertical), and legendTitle: false
  • background, panelBackground, and panelBorder
  • grid, gridColor, axisLine, and ticks
  • xLabelAngle and yLabelAngle, which turn axis labels anticlockwise as element_text(angle =) does
  • titleAnchor and padding
  • captionSize, captionColor, and captionAnchor, which style labs(caption:)

config: accepts raw Vega-Lite config, merged last. Calls without a preset build on the current theme.

use Phiz\Datasets;
use Phiz\Plot;

$plot = Plot::of(Datasets::mpg())
    ->aes(x: 'displ', y: 'hwy', color: 'drv')
    ->point()
    ->theme('minimal', legendPosition: 'bottom', font: 'Georgia, serif', config: ['axis' => ['labelColor' => '#666666']])
    ->labs(title: 'A minimal theme with the legend below');

A page can also choose a theme at render time. The vega-embed option theme accepts the vega-themes names, and phiz adds 'auto', which follows the reader’s light or dark mode preference:

$plot->embed(['theme' => 'auto']);

From phpstats/phiz@bc7ed3e.