liveBook · Manning

Logarithmic scale

As explained in D3.js in Action, Third Edition

A logarithmic scale maps values through a logarithmic transformation so multiplicative differences in magnitude can be represented across a visual range.

A logarithmic scale maps a continuous domain through a logarithmic transformation. It is useful when values span large differences in magnitude, allowing multiplicative changes to occupy a readable visual range. section 7.5 section 7.4.1

Cetacean population scatterplot

A linear x-axis compresses species with smaller populations, making their values difficult to read. Replacing it with a logarithmic scale displays tenfold steps such as 1, 10, 100, 1k, 10k, 100k, 1M, and 10M, while preserving the full population range. section 7.4.1

const maxPopulation = d3.max(data, d => d.global_population_estimate);
xScale = d3.scaleLog()
  .domain([1, maxPopulation])
  .range([0, innerWidth])
  .nice();

Configure the scale with the dataset’s maximum population, map the domain from 1 to that maximum, and spread it across the chart width. section 7.4.1

How D3.js in Action, Third Edition covers it

How deep it goes
Defines logarithmic transformation and demonstrates domain, range, and nice() configuration, without developing the underlying mathematics.
Examples
Uses cetacean population estimates and tenfold axis labels from 1 through 10M to demonstrate the visual benefit.
What it assumes
Assumes familiarity with linear and ordinal scales while introducing logarithmic scaling for the scatterplot.
What you can do after
Apply it via D3 by configuring d3.scaleLog() with a data-driven domain and chart-width range.
How it connects
Connects logarithmic scales with continuous domains, linear scales, radial scales, D3, and scatterplots.

Also explained in

Compare how 3 books explain logarithmic scale →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.