Tutorials › Pharmacometric Workflows and Reproducible Modeling › Applying Consistent Plot Themes and Scales

Automating Diagnostics and Reports · Tutorial 281 of 399

Applying Consistent Plot Themes and Scales

Build a versioned plotting specification that makes diagnostic figures readable, reproducible, and visually comparable without changing what the data show.

Intermediate 10 min read

What You'll Learn

  • Distinguish plot themes, which control appearance, from scales, which map data values to visual positions and marks.
  • Set stable colors and symbols for categories across plots and report runs.
  • Choose common axis limits and breaks that preserve units and support panel comparisons.
  • Specify readable typography, legends, and physical panel dimensions for output at a known size.
  • Detect inconsistent mappings, clipped data, crowded labels, and other plotting failures.
  • Record and test a reusable specification so automated figures remain reproducible.

Why Consistent Plot Defaults Matter

A diagnostic report may contain many plots generated from different outputs, subgroups, or simulation scenarios. If each plot chooses colors, axis limits, fonts, and dimensions independently, the reader must repeatedly decode the visual system. Worse, the same category or numerical value can look different from panel to panel. A reusable plot specification reduces this variation: it makes figures easier to scan and helps comparisons reflect the data rather than accidental formatting choices.

Consistency is not the same as making every plot identical. A diagnostic figure may need a particular scale, reference line, or layout because of the variables it displays. The goal is to define defaults and clear exceptions, then apply them predictably. These choices govern presentation; they do not establish whether a model is adequate or what a diagnostic pattern means.

Definition: A plot theme is a reusable set of presentation settings, such as fonts, text sizes, background, grid lines, legend appearance, and spacing. A scale maps data values or categories to visual properties such as position, color, or symbol, and specifies features such as limits, breaks, and labels. A plot specification combines these settings with any intentional plot-specific overrides so that a plotting workflow can apply them consistently.

Separate Appearance From Data Mapping

A theme controls how plot elements look, not what data values they represent. It may set a plain background, a sans-serif typeface, the size of axis labels, or the placement of a legend. A scale controls how values are represented. For example, an x-axis scale determines where time values appear, while a color scale determines which color represents each run or group.

Keeping these roles distinct makes a specification easier to check. A font-size change should not change the values plotted. A scale change can affect the visible positions or range and therefore needs more substantive review. In particular, a scale limit that removes observations is not merely a cosmetic adjustment.

For categorical variables, define the mapping once and keep it fixed. A category should not switch colors because its order changed, because a subset happens not to contain another category, or because a new report was generated. Where color alone may be difficult to distinguish, use a second visual cue, such as symbol shape or line type. Use direct, informative labels in legends rather than relying on color names alone.

For quantitative axes, choose units, transformation, limits, breaks, and labels deliberately. A time axis in hours should remain in hours across plots intended for comparison. A logarithmic axis requires positive values and a clear statement of the displayed units and transformation. Avoid selecting a different range for each panel when readers are expected to compare magnitudes across panels.

Worked Example: Stable Colors and Symbols Across Runs

Hypothetical data. An automated report plots prediction residuals from three analysis runs named Base, Covariate, and Sensitivity. Each run appears in multiple panels. The report specification assigns Base to dark blue circles, Covariate to orange triangles, and Sensitivity to purple squares.

The mapping is stored by category name, not by the order in which categories happen to appear. Thus, even if one panel contains only Base and Sensitivity, Base remains dark blue and Sensitivity remains purple. A legend lists all three labels in the same order when all three are represented.

Suppose a report update adds a fourth run, Robustness. The correct action is to assign it a distinct, accessible color-symbol combination in the specification and test that mapping. It is not to recalculate the color sequence independently for each plot. No arithmetic is needed to obtain the mapping: its reproducibility comes from explicitly associating each named category with a fixed visual key.

The reader can now distinguish runs by both color and shape. This redundancy helps when a figure is printed in grayscale or viewed by someone with color-vision differences. It does not imply that the runs have an ordering or that one is better than another; those conclusions require analysis of the plotted quantities.

Shared Scales Make Comparisons Interpretable

When panels show the same quantity in the same units, a common axis range supports direct visual comparison. The reason can be expressed directly. For a linear axis with lower limit \(a\) and upper limit \(b\), the relative position of a value \(x\) within the plotting range is

$$ u = \frac{x-a}{b-a}. $$

If two panels use the same \(a\) and \(b\), the same value \(x\) has the same relative position \(u\) in both. If the limits differ, that position can differ even though the value is identical. Therefore, matching units and limits is a necessary visual condition for comparing positions on linear axes. It does not guarantee a fair comparison if the plotted quantities or transformations differ.

A useful default for a signed quantity centered on a meaningful zero, such as a residual, is a symmetric range around zero. A shared symmetric range also keeps zero in the same position in each panel. Limits should include the data being displayed, with enough room for points and labels at the edges. Breaks should be readable and consistent; they need not occur at every possible value.

There are cases where shared scales are not suitable. Panels may show different units or quantities, or one extreme value may make the rest of the data unreadable. In that case, use separate scales only when the purpose of the figure is clear, make the distinction visible in labels or captions, and do not invite readers to compare heights or distances as if the scales matched. A free scale improves within-panel resolution but sacrifices direct between-panel comparison.

Worked Example: Choosing Common Limits for Residual Panels

Hypothetical data. Two panels show residuals in the same units. Panel A ranges from \(-0.16\) to \(0.21\); Panel B ranges from \(-0.24\) to \(0.18\). The intended comparison is the magnitude and direction of residuals across panels, so use a common symmetric linear scale centered at zero.

First find the largest absolute endpoint: \(\max(0.16, 0.21, 0.24, 0.18)=0.24\). Add 10% headroom: \(0.24 \times 1.10=0.264\). Round outward to a convenient limit of \(0.30\), giving common axis limits \(-0.30\) and \(0.30\).

For example, set labeled breaks at \(-0.2\), \(-0.1\), \(0\), \(0.1\), and \(0.2\), and label the axis with the residual units. Both panels use exactly these limits and breaks. The rounding leaves visible space around the largest observed magnitude, \(0.24\), and zero lies at the center of each plotting region.

The panel-specific ranges would place the same value at different relative positions, undermining the intended comparison. The common range preserves both endpoints of the stated data. If a later run contains a residual of \(0.34\), the specification must be reviewed and updated for all comparable panels, or the plot must state clearly that the value falls outside a deliberately chosen display range. Silently clipping it would conceal data.

Typography, Legends, and Physical Dimensions

A theme should specify a font family available in the rendering environment, as well as sizes for titles, axis titles, tick labels, legend text, and annotations. Relative hierarchy matters: plot titles should be easy to find, while tick labels must remain legible at the final displayed size. Extremely small text can make a multi-panel report technically complete but practically unusable. Test the output at its intended viewing size, not only while zoomed in on a development screen.

Legend design also benefits from explicit defaults. Set a consistent title, ordering, position, and key size. Place the legend where it does not cover observations or force plot panels to become too narrow. If a legend is repeated in every panel without adding information, a shared legend may save space; if a panel is viewed separately, a local legend may be more useful. The specification should make that choice predictable.

Panel dimensions are physical dimensions, not just proportions in a software window. Specify a target width and height for each panel, or a fixed overall figure size and layout. Then verify that labels, legends, and annotations fit. Saving at higher resolution cannot repair text that is too small in physical size, and changing the figure dimensions after setting them can alter the layout. A stable output size is especially important when figures are assembled into automated reports.

Worked Example: Calculating a Multi-Panel Figure Size

Hypothetical layout. A report needs six plots arranged in three columns and two rows. Specify each panel as 3.2 inches wide by 2.6 inches high, with a horizontal gap of 0.18 inches and a vertical gap of 0.22 inches.

The panel area is \(3(3.2)=9.6\) inches wide and \(2(2.6)=5.2\) inches high. There are two horizontal gaps and one vertical gap, so the plot-grid dimensions are

$$ W = 3(3.2) + 2(0.18) = 9.96\ \text{inches}, \qquad H = 2(2.6) + 0.22 = 5.42\ \text{inches}. $$

If a shared legend below the grid requires 0.55 inches, the planned figure height becomes \(5.42+0.55=5.97\) inches, before any additional outer margins. The specification should record the panel dimensions, gaps, legend placement, and final output dimensions. After rendering, check that the legend and labels fit without overlap and that the smallest text remains readable at approximately 9.96 by 5.97 inches.

If an added annotation requires more room, revise the layout deliberately rather than shrinking all text until it fits. The resulting size and text settings should be applied to comparable reports so that visual differences do not arise solely from ad hoc resizing.

Implementing and Checking a Reusable Specification

A practical configuration records the visual rules as named settings, separately from the data transformations and plot-specific content. The following process helps make the choices reviewable and repeatable.

1
Define the visual contract.
List the category mappings, axis units and transformations, default limits and breaks, fonts and text sizes, legend behavior, and panel dimensions required for the report.
2
Apply defaults and declare exceptions.
Use the shared settings for comparable plots. Document a deliberate exception, such as a different unit or a free scale, close to the plot specification rather than allowing it to occur implicitly.
3
Render representative cases.
Check a typical panel, sparse data, all categories, long labels, and values near the limits. Inspect the saved output at its target physical size.
4
Record and test the configuration.
Version the specification with the report workflow. Check that category mappings, units, limits, breaks, dimensions, and output format have not changed unexpectedly between runs.

Useful failure checks include missing categories in a legend, colors that change when a category is absent, labels that overlap, clipped points, axes with inconsistent units, and panels that silently use different limits. Also check whether a requested logarithmic scale contains zero or negative values: these values cannot be represented on the ordinary logarithmic scale. A plot that fails such checks should trigger a clear warning or documented exception, not a quiet change to its appearance.

Two common approaches deserve distinction. Fixed scales favor direct comparisons across panels, while free scales favor detail within each panel. Neither is universally correct; the choice depends on the comparison the report is intended to support. Similarly, a global theme is convenient for routine defaults, while explicit per-plot settings offer control for special cases. Unrecorded per-plot settings are harder to reproduce and audit. Use the global configuration as the default and make any necessary override visible.

Common Pitfalls and Limitations

  • Changing colors with category order. A palette applied by position can give a category a different color when data are filtered or reordered. Map colors and symbols by stable category names.
  • Comparing panels with different limits. Identical values can occupy different positions on separately scaled axes. Use common limits when the panels are meant for direct magnitude comparison, or disclose why scales differ.
  • Setting limits that discard data. A display setting may remove out-of-range observations rather than merely zooming the view. Check the data range before rendering and report any intentional exclusion.
  • Using a default transformation without checking its domain. A logarithmic scale cannot display zero or negative values on the ordinary log scale. Choose a suitable scale and explain it rather than silently modifying values.
  • Assuming one theme fits every output. A layout that works on a large screen may fail in a narrow report column. Fix the output size and inspect the rendered figure in its intended context.

Consistent themes and scales improve legibility and comparability, but they cannot correct data errors, make an unsuitable plot informative, or establish a scientific conclusion. They are part of reproducible reporting: explicit choices make the display easier to inspect, rerun, and maintain.

Check Your Understanding

Answer each question, then compare your reasoning with the solutions.

  1. What is the difference between a plot theme and a scale?
  2. Two comparable panels show the same quantity in the same units. Why can different axis limits mislead a visual comparison?
  3. Residuals in two panels range from \(-0.12\) to \(0.17\) and from \(-0.20\) to \(0.09\). Find a symmetric common limit using 10% headroom and round outward to the nearest 0.05.
  4. Why should a color for a named run be specified by category rather than by its position in a list?
  5. Name two checks to perform on a rendered figure saved at its intended dimensions.

Solutions. (1) A theme sets presentation, such as fonts and backgrounds; a scale maps data values or categories to visual properties and defines features such as limits and breaks. (2) The same value can occupy a different relative position when the limits differ, so visual distances are not directly comparable. (3) The largest absolute endpoint is \(0.20\). With 10% headroom, \(0.20 \times 1.10=0.22\); rounding outward to the nearest 0.05 gives a limit of \(0.25\), so use \(-0.25\) to \(0.25\). (4) Category order can change across plots or runs, while a name-based mapping preserves the same visual identity. (5) For example, check that text is readable and that labels or legends do not overlap; also check for clipped data and consistent limits where panels are meant to be compared.

Clinical Biostats