Styling marks from data

A mark’s colour, size, shape, width and opacity from its data: each a style attribute, holding a look, a list of looks, or a reading applied with -> to a map, a ramp or an expression.

On this page

#Style attributes

A data mark has up to six style attributes — colour, size, shape, width, opacity and style — each an expression whose result is one look for every item of the mark. A style attribute holds a plain look, a list of looks with one per item, a column whose cells are looks, or a reading applied with -> to a record, a ramp or an expression of _. It takes the forms that mean something for its mark — a shape has no ramp — and nothing else. The value written says which it is.

scatter(x: #trial.time; y: #trial.id; colour: teal)
scatter(x: #trial.time; y: #trial.id; colour: teal, teal, red, teal)
scatter(x: #trial.time; y: #trial.id; colour: #trial.status -> (event: teal; censored: red))
scatter(x: #trial.time; y: #trial.id; colour: #trial.dose -> 0..10 -> temperature)

style is the pattern an item is drawn in. On a bar, histogram, boxplot or violin it is a fill, solid, hatched, dotted or outlined; on a lollipop's stems it is a stroke, solid, dashed or dotted. Here the forecast months are hatched:

Nine monthly sales bars in one blue, the six recorded months filled solid and the three forecast months hatched, with a key pairing recorded with a solid square and forecast with a hatched one.Nine monthly sales bars in one blue, the six recorded months filled solid and the three forecast months hatched, with a key pairing recorded with a solid square and forecast with a hatched one.
@data#sales{
    month | units | kind
    Jan   | 42    | recorded
    Feb   | 45    | recorded
    Mar   | 51    | recorded
    Apr   | 49    | recorded
    May   | 56    | recorded
    Jun   | 60    | recorded
    Jul   | 63    | forecast
    Aug   | 66    | forecast
    Sep   | 68    | forecast
}

graph{
    bar(
        x: #sales.month
        y: #sales.units
        colour: blue
        style: #sales.kind -> (recorded: solid; forecast: hatched)
    )
}(
    legend
    x format: months
    y axis label: units (thousands)
)

A mark drawn as one stroke — a line, a curve, an area, a density or a fit — has no separate items, so its colour, width and styles are each a function of the point they are at, written in its coordinates x and y as a curve's equation is. Applied to a record of intervals, the look changes exactly where the coordinate crosses a key's boundary, however finely the mark is drawn. Here a record becomes a projection at week 8, and a curve is red wherever it is below zero:

Two graphs. On the left, a pink line of wheat heights solid to week 8 and dashed beyond it. On the right, the cubic x³ − 3x, teal where it is above zero and red where it dips below, the colour changing exactly at each crossing.Two graphs. On the left, a pink line of wheat heights solid to week 8 and dashed beyond it. On the right, the cubic x³ − 3x, teal where it is above zero and red where it dips below, the colour changing exactly at each crossing.
@data#wheat{
    week | height
    0    | 4
    2    | 11
    4    | 22
    6    | 35
    8    | 47
    10   | 58
    12   | 66
}

grid{
    graph{
        line(x: #wheat.week; y: #wheat.height; style: x -> (..8: solid; 8..: dashed))
    }(
        x axis label: week
        y axis label: height (cm)
    )
    graph{
        curve(f: x^3 - 3x; colour: y -> (..<0: red; 0..: teal))
    }(
        x domain: [-2.5, 2.5]
        y domain: [-4, 4]
    )
}

A curve written in t reads its t as well, and one on the polar plane its r and theta. A look that reads none of them, a plain look or a slider's letter applied to a record, is the same everywhere along the mark. A list is refused: a function of the point has one look at each point, not one per item.

Style attributes are not animatable: !cue.to on one is refused. A look that changes as the scene plays follows a parameter instead, in a band's end or in an expression of _ (below).

#A plain value, a list, or a column

A plain value draws every item alike: colour: teal, size: 8, shape: cross. A colour is one of the named hues or a literal, rgb(120, 120, 120) or hex(787878). A list gives one per marker, bar, bin, stem, row or region in written order, starting again from the first when the list is shorter: colour: grey, grey, grey, red, grey picks out the fourth bar, and size: 4, 8, 12 sizes three markers.

A column of a @data dataset is a list like any other, one look per row, so it fits where its cells are looks: colour: #trial.hue for a column of colour names, size: #cities.size for a column of sizes. A column of anything else is refused, naming the first row that is not a look. To draw categories or numbers, apply the column to a record or a ramp.

scatter(x: #trial.time; y: #trial.id; shape: #trial.dose)

[attribute]node `scatter`: attribute `shape`: `#trial.dose`, row 1: `2` is not one of the allowed values: circle, square, rectangle, triangle, star, diamond, hexagon, cross, tick

#Applying a reading: reading -> look

reading -> f applies f to a reading. On the left is a column, or one of the mark's own readings, #self.value; on the right, what turns each reading into a look: a record from keys to looks, an interval and a ramp, or an expression of _, the reading coming in. Applied to a list, it is applied to each item, and the result is one look per item. -> is an operator of the expression language (Expressions) and reads left to right, so #weather.temp -> -10..40 -> temperature places each reading along the interval, then colours that place.

colour: #trial.status -> (event: teal; censored: red)
colour: #self.value -> (..<0: purple; 0..: pink)
colour: #weather.temp -> -10..40 -> temperature
size: #self.degree -> 1..10 -> 1 + 2_

A record is written as an attribute group is: a key, a colon and its look, separated by semicolons. A key may be several words, other road: grey, and a look keeps its own commas, so rgb(120, 120, 120) stays one piece. Each reading takes the look of the first key that holds it, else the record's otherwise. A reading no key holds, in a record with no otherwise, has no look, and its item is drawn in the mark's default.

#Columns and a mark’s own readings

  • A column of a @data dataset, #trial.status, read row by row alongside the mark's own columns. A coordinate is applied as the column it came from: colour: #trial.time -> (..<8: grey; otherwise: teal).
  • A reading the mark publishes for each of its items, written with #self. On a scatter, #self.value is the reading, measured along the axis the readings are; on a bar or lollipop, the bar's value; on a histogram, a bin's count; on a contour, each level's value; on a surface or field, the value at each point.
  • On a line, curve, area, density or fit, no #self: the coordinates of the point the look is at, written bare — x and y, and on a curve its t, r and theta.
  • On a network's vertices: #self.group, #self.component and #self.community, its classes, and #self.degree, #self.in-degree, #self.out-degree, #self.betweenness and #self.weight, its measurements. On its edges: #self.weight.
  • An expression over any of those, item by item: colour: #sales.base + #sales.growth -> (..<100: grey; otherwise: teal), size: #cities.people -> _ / 1000 -> (..<1: 4; otherwise: 9), colour: #self.degree -> _ > 3 -> (true: red; false: grey).

A heatmap and a choropleth read their numbers through their value attribute, so their colour applies that column itself: colour: #corr.r -> -1..1 -> temperature. A reading a mark does not publish is refused: on a scatter, colour: #self.speed -> (0..1: red) is `scatter` declares no attribute `speed`, so `#self.speed` has no value.

#Categories

Names as keys are categories. Each item whose reading is a name takes that name's look; names are matched ignoring case and spacing, so a record that keys both Blue and blue is refused. A name the record leaves out takes the record's otherwise, so colour: #votes.party -> (Green: teal; otherwise: grey) picks out one party against the rest; without an otherwise it keeps the mark's default look. A column of codes reads as names too: #mp.event -> (1: teal; 0: red).

Two records applied to the same reading style one dataset twice over. Here each subject's last reading is a teal circle where the event was seen and a red cross where follow-up stopped:

Eight subjects as horizontal lines from week 0, each ending in a teal circle where the event was seen or a red cross where follow-up stopped, with a key naming event and censored beside their markers.Eight subjects as horizontal lines from week 0, each ending in a teal circle where the event was seen or a red cross where follow-up stopped, with a key naming event and censored beside their markers.
@data#trial{
    id | time | status
    1  | 6    | event
    2  | 9    | censored
    3  | 10   | event
    4  | 13   | event
    5  | 17   | censored
    6  | 22   | event
    7  | 25   | censored
    8  | 32   | censored
}

graph{
    @for(n: #trial; as: row){
        segment(start: (0, {{row.id}}); end: ({{row.time}}, {{row.id}}); colour: neutral)
    }
    scatter(
        x: #trial.time
        y: #trial.id
        colour: #trial.status -> (event: teal; censored: red)
        shape: #trial.status -> (event: circle; censored: cross)
    )
}(
    x axis label: weeks
    y axis label: subject
)

#Bands

Intervals as keys are bands, written with two dots: 0..25 holds both ends, and an end left out runs on for ever, ..18.5 or 30... A < beside the dots leaves that end out: ..<18.5 is everything below 18.5, 18.5..<25 runs from 18.5 up to but not including 25, and 25<.. is everything above 25. So in (..<18.5: blue; 18.5..<25: green; 25..: red) 18.5 is green and 25 red. Where two bands do share a reading, the band written first has it, and a reading no band holds takes the record's otherwise, or without one is drawn in the mark's default look. One interval to one colour is a band like any other: #self.value -> (0..2: teal) colours the readings from 0 to 2 and leaves the rest to the otherwise or the default. The key writes an end a band leaves out with < or >, and one it holds with ≤ or ≥.

The ends may be expressions of the scene's parameters, so a slider can move a threshold and the items change as it passes them:

Ten comparisons along a scale from −30 to 30, purple below a cut-off of 0 and pink at or above it, with a cut-off slider beneath and a key reading < 0 and ≥ 0.Ten comparisons along a scale from −30 to 30, purple below a cut-off of 0 and pink at or above it, with a cut-off slider beneath and a key reading < 0 and ≥ 0.
graph:x{
    slider:c(name: cut-off; range: [-20, 20]; step: 1; default: 0)
    scatter(
        x: -24, -15, -9, -4, -1, 2, 6, 11, 18, 27
        colour: #self.value -> (..<c: purple; c..: pink)
    )
}(
    x axis label: change
)

A record's keys are all names or all intervals. A column of names applied to bands is refused, naming the first row that is not a number.

#Ramps

A ramp colours each item by its reading's place along a closed interval: #weather.temp -> -10..40 -> temperature. Applied to an interval, a reading becomes its place from 0 at the low end to 1 at the high end, and a reading outside the interval takes the nearer end. Applied to a ramp, that place becomes a colour. The ramps are multicolour, violet low to red high, temperature, blue through white to red, and the nine hues blue, red, orange, yellow, teal, green, pink, purple and grey, each run from pale to full. Below, the colour runs from 10 °C to 30 °C on the temperature ramp while the size follows population.

Six cities placed by rainfall and sunshine, each a disc that grows with population and whose colour runs from blue for the coldest to red for the hottest on the temperature ramp.Six cities placed by rainfall and sunshine, each a disc that grows with population and whose colour runs from blue for the coldest to red for the hottest on the temperature ramp.
@data#cities{
    city      | rain | sun  | temp | people
    Hobart    | 616  | 2263 | 12.7 | 0.25
    Melbourne | 603  | 2363 | 15.9 | 5.2
    Sydney    | 1215 | 2636 | 18.6 | 5.4
    Perth     | 734  | 3229 | 18.8 | 2.3
    Brisbane  | 1011 | 2884 | 21.4 | 2.7
    Darwin    | 1731 | 3022 | 27.6 | 0.15
}

graph{
    scatter(
        x: #cities.rain
        y: #cities.sun
        colour: #cities.temp -> 10..30 -> temperature
        size: #cities.people -> 0..6 -> 4 + 20_
    )
}(
    x axis label: rain, mm a year
    y axis label: sunshine, hours a year
)

A heatmap, choropleth, surface, contour and field each read a number for every cell, region, level or point, so a plain colour is the ramp itself — a ramp's name, colour: temperature; one hue run from pale to full, colour: blue; or a list of stops low end first, colour: teal, yellow, red — over the least and greatest readings drawn, moving as they do. Applying the readings to an interval and a ramp pins the window instead: on a surface, contour or field colour: #self.value -> -1..1 -> temperature, and on a heatmap or choropleth the value column, colour: #corr.r -> -1..1 -> temperature. A record on a heatmap, a choropleth or a contour paints flat in its keys' looks: each cell, region or level in the colour its band or category gives it. invert reverses whichever ramp is drawn.

#Sizes and widths

A size scale is a reading applied to an interval and then to an expression of _, the reading's place along it: size: #cities.people -> 0..6 -> 4 + 20_ draws a reading of 0 at size 4 and 6 at size 24, in a straight line between them, and a reading outside 0 to 6 at the nearer end's size. The expression may name a parameter: size: #self.value -> _ * k, with k a slider's letter. Intervals mapped to one size each band the readings: size: #self.value -> (..<20: 4; otherwise: 9). Opacity scales the same way, from 0 to 1: opacity: #trial.dose -> 0..10 -> 0.3 + 0.7_. The sizes are in the mark's own units — a scatter's and a marker's size, multiples of the plain vertex, and pixels of stroke for an edge's width.

Seven people in two teams: the design team teal squares, the research team orange circles, each vertex larger the more people it works with, Mia largest at the centre.Seven people in two teams: the design team teal squares, the research team orange circles, each vertex larger the more people it works with, Mia largest at the centre.
@data#people{
    name  | team
    Ana   | design
    Ben   | design
    Chen  | design
    Mia   | research
    Omar  | research
    Pia   | research
    Raj   | research
}

@data#ties{
    a    | b
    Ana  | Ben
    Ana  | Chen
    Ben  | Chen
    Chen | Mia
    Mia  | Omar
    Mia  | Pia
    Mia  | Raj
    Omar | Pia
}

network{
    vertices(
        names: #people.name
        colour: #people.team -> (design: teal; research: orange)
        shape: #people.team -> (design: square; research: circle)
        size: #self.degree -> 1..4 -> 1 + 1.2_
    )
    edges(from: #ties.a; to: #ties.b)
}

#Which marks take which style attributes

  • scatter: colour, size, shape and opacity, following a column or #self.value.
  • bar: colour, opacity and style, following a column or #self.value.
  • lollipop: colour, size for its dots, width and style for its stems, and opacity.
  • histogram: colour and style, following a bin's count (#self.value). A column gives a bin nothing, since a bin is no one row.
  • boxplot and violin: colour and style, a look or a list with one per box, nothing applied.
  • segment: colour, one per segment where its ends are lists of points — a look, a list, or a list another mark publishes, applied: #trend.residuals -> (..<0: blue; 0..: red) from a fit.
  • line and fit: colour, width and style; curve: colour, width and stroke-style; area and density: colour, width, stroke-style and fill-style — each a function of the point along the mark (x, y, …), never a list or a column. A curve written as an equation takes plain looks only.
  • heatmap and choropleth: colour, a ramp over their readings, or their value column (or another column) applied to a window and a ramp, to bands or to categories.
  • surface, contour and field: colour, a ramp over their readings, or #self.value applied to a window and a ramp; a contour also takes bands.
  • marker: colour, size and opacity, following a column.
  • vertices: colour, size and shape, following a column, a class (#self.group, #self.component, #self.community) or a measurement (#self.degree, #self.betweenness, …).
  • edges: colour and width, following a column or #self.weight; colour-from colours each edge from the vertices it joins instead, ends or gradient.
  • vertex and edge: plain values, which win over whatever their table gives them.

A shape a mark does not draw is refused by name with the shapes it has: a vertex's are circle, square, diamond and none.

#The key, and group:

Every key of a style attribute's record is an entry in the scene's key, beside its look, under its name or its interval: < 0, 0 – 25, ≥ 30. Everything a record's otherwise takes is one entry, other. Where a colour record and a shape record are applied to the same reading, a name is one entry, its marker in its colour. A size or width scale applied to a mark's own reading, #self.degree -> 1..10 -> 1 + 2_, is a scale row from the interval's low end to its high end with the size drawn at each; a column's scale is worked out while the document compiles and earns no row. The ramp of a heatmap, surface, field or choropleth is a scale row of its own. A graph draws its key only once legend is written on it; on any other environment !legend turns the key off.

A record styles the items of one mark. group: on a mark, with the graph's key:, does a different job: it puts several marks under one entry. The key is a record from each group to a colour, a style or both — key: (Projected: red dashed; Forecast: hatched), and its otherwise draws every group it does not name, under one entry, other. A mark's own colour or style, a record included, wins over what its group pins.

#Writing guidance

  • Framing a readable graph — When to write a domain, the defaults left alone, naming a series, the ticks the prose reads, and one meaning per hue.

Last updated