mpg

Fuel economy of 30 cars (small fuel-economy reference dataset).

Each row is one vehicle. Columns:

The bundled literal is stored in column-store form; plot accepts either column-store or row-store input and normalises internally.

Source: subset of the mpg dataset bundled with the ggplot2 R package; see help("mpg") in R. Originally extracted from the US Environmental Protection Agency’s fuel economy data, https://fueleconomy.gov/, restricted to models with a new release every year between 1999 and 2008.

Usage

mpg

Examples

Preview the first ten entries (out of 30).

#let cols = mpg.pairs()
#let cell(v) = if v == none { text(fill: gray, [_none_]) } else { [#v] }
#table(
  columns: cols.len(),
  align: center + horizon,
  inset: 5pt,
  stroke: 0.5pt,
  ..cols.map(p => strong(raw(p.at(0)))),
  ..range(10).map(i => cols.map(p => cell(p.at(1).at(i)))).flatten(),
)

Table of the first ten rows of the mpg dataset with seven columns: manufacturer, model, displ, cyl, class, cty, hwy.

Table of the first ten rows of the mpg dataset with seven columns: manufacturer, model, displ, cyl, class, cty, hwy.

Highway mpg vs engine displacement, filled by vehicle class.

#plot(
  data: mpg,
  mapping: aes(x: "displ", y: "hwy", fill: "class"),
  layers: (geom-point(size: 3pt),),
  width: 11cm,
  height: 7cm,
)

Scatter chart of highway miles per gallon against engine displacement (litres) for 30 vehicles, with points filled by vehicle class; mpg falls as displacement rises.

Scatter chart of highway miles per gallon against engine displacement (litres) for 30 vehicles, with points filled by vehicle class; mpg falls as displacement rises.

Facet by class and add a linear smoother per panel to compare per-class trends.

#plot(
  data: mpg,
  mapping: aes(x: "displ", y: "hwy"),
  layers: (
    geom-point(size: 2pt),
    geom-smooth(method: "lm", se: false),
  ),
  facet: facet-wrap("class", ncolumn: 3),
  width: 12cm,
  height: 7cm,
)

Faceted scatter chart of highway mpg against engine displacement split into one panel per vehicle class, each panel overlaid with a linear regression line.

Faceted scatter chart of highway mpg against engine displacement split into one panel per vehicle class, each panel overlaid with a linear regression line.

Back to top