Skip to content

← Examples

Ridgeline Chart ​

A ridgeline chart of Seattle's daily high temperatures by month, with each month's density silhouette overlapping the row above, a thin rule under every baseline, and month names in the left margin instead of a shared y axis.

ts
import { Spacing, Unit, chart, datum, derive, field, kde, layer, rect, ribbon, spread } from "gofish-graphics";
import { seattleWeather } from "./dataset";

const monthNames = [
  "Jan",
  "Feb",
  "Mar",
  "Apr",
  "May",
  "Jun",
  "Jul",
  "Aug",
  "Sep",
  "Oct",
  "Nov",
  "Dec",
];

const days = seattleWeather.map((d) => ({
  ...d,
  month: monthNames[new Date(d.date).getUTCMonth()],
}));

const ruleFrom = -10;

const ruleTo = 40;

const rowPitch = 24;

const ridgeHeight = 140;

const container = document.getElementById("app");

const temp = field("temp_max");
chart(days, {
  axes: { x: true, y: false },
  schema: { temp_max: { name: "Max temperature", unit: Unit("°C") } },
  w: 500,
})
  .flow(
    spread({
      by: field("month").sort(monthNames),
      dir: "y",
      spacing: Spacing.pitch(rowPitch, { at: "baseline" }),
    }),
    derive(kde("temp_max"))
  )
  .mark(
    layer({ h: ridgeHeight }, [
      ribbon({
        x: temp.domain(),
        h: temp.density(),
        fill: "steelblue",
        stroke: "white",
        strokeWidth: 1,
        opacity: 0.85,
        blendMode: "normal",
      }),
    ])
  )
  // Per-row baseline labeling, in the style of a ggridges ridgeline: a
  // thin rule along each month's baseline with the month name to the
  // LEFT of the plot (tick-label style), instead of a standard y axis
  // that can't line up with 12 overlapping, unevenly-tall silhouettes.
  // The rules are the same fixed-pitch baseline spread as the ridges, a
  // bare rect per month, so each registers exactly on its ribbon's zero
  // line. Each rule spans the x axis. Each rule carries its month as a
  // label outside its left end, standing on the rule. `.zOrder(-1)` paints the rules BEHIND the
  // ribbons, visible only outside the silhouettes, the classic look.
  .layer(
    chart(monthNames.map((month) => ({ month })))
      .flow(
        spread({
          by: field("month").sort(monthNames),
          dir: "y",
          spacing: Spacing.pitch(rowPitch, { at: "baseline" }),
        })
      )
      .mark(
        rect({
          x: datum(ruleFrom),
          w: datum(ruleTo - ruleFrom),
          h: 1,
          fill: "#999",
        }).label("month", {
          position: "outset-left-end",
          fontSize: 11,
          color: "#666",
        })
      )
      .zOrder(-1)
  )
  .render(container);
dataset.ts
ts
export type SeattleWeather = {
  date: string;
  precipitation: number;
  temp_max: number;
  temp_min: number;
  wind: number;
  weather: string;
};
export const seattleWeather: SeattleWeather[] = [
  { date: "2012-01-01T00:00:00.000Z", precipitation: 0, temp_max: 12.8, temp_min: 5, wind: 4.7, weather: "drizzle" },
  { date: "2012-01-02T00:00:00.000Z", precipitation: 10.9, temp_max: 10.6, temp_min: 2.8, wind: 4.5, weather: "rain" },
  { date: "2012-01-03T00:00:00.000Z", precipitation: 0.8, temp_max: 11.7, temp_min: 7.2, wind: 2.3, weather: "rain" },
  { date: "2012-01-04T00:00:00.000Z", precipitation: 20.3, temp_max: 12.2, temp_min: 5.6, wind: 4.7, weather: "rain" },
  { date: "2012-01-05T00:00:00.000Z", precipitation: 1.3, temp_max: 8.9, temp_min: 2.8, wind: 6.1, weather: "rain" },
  // … 1,456 more rows
];