Skip to main content

33  Zoom, fits, and annotations

library(rtemis.draw)

Every ECharts chart with continuous or long category axes takes zoom, and every cartesian chart takes fitted curves and annotations computed outside the drawing.

33.1 Zoom

zoom = TRUE adds zoom controls suited to the chart. Line charts, histograms, densities, and Manhattan plots get a slider below the x-axis; bar charts and boxplots get a slider on the category axis. Scatter and volcano plots zoom both axes and add box-zoom, undo, and reset buttons in the top-right corner.

In every chart, Shift+scroll zooms at the pointer and dragging pans; a plain scroll and a one-finger touch drag scroll the page past the chart, and a two-finger pinch zooms. Double-click resets the view. Axis ends are labeled only where they fall on a tick.

draw_scatter(
  penguins$bill_len,
  penguins$flipper_len,
  group = penguins$species,
  zoom = TRUE
)
draw_histogram(
  penguins$body_mass,
  group = penguins$species,
  zoom = TRUE
)
2026-11-18 17:29:49 Removed 2 NA values from x [distribution_samples]

With square = TRUE or equal_axes = TRUE, scroll and drag change both axes by the same factor, so one data unit keeps the same length on both axes; the buttons omit box zoom, whose rectangle sets each axis independently. Pass DataZoom() objects instead of TRUE for full control over the zoom components. SVG export omits zoom controls and shows the full data range.

33.2 Supplied fits and annotations

Materialize predictions from any fitting procedure, then add them to a drawing. This example uses a quadratic model of stopping distance:

model <- lm(dist ~ poly(speed, 2), data = cars)
new <- data.frame(speed = seq(min(cars$speed), max(cars$speed), length.out = 100))
prediction <- predict(model, newdata = new, interval = "confidence")
figure <- draw_scatter(cars$speed, cars$dist, rug = TRUE,
                      xlab = "Speed (mph)", ylab = "Stopping distance (ft)")
figure <- draw_add_fit(figure, new$speed, prediction[, "fit"],
  lower = prediction[, "lwr"], upper = prediction[, "upr"], name = "Quadratic fit")
draw_annotate(figure, hline = 50, x = 8, y = 55, text = "50 ft reference")

For comparison, draw_scatter() estimates and draws a fit in a single call. Here fit = "gam" fits a GAM smooth with mgcv::gam() and shades a 95% confidence band (1.96 standard errors):

draw_scatter(cars$speed, cars$dist, rug = TRUE, fit = "gam", fit_name = "GAM fit",
             xlab = "Speed (mph)", ylab = "Stopping distance (ft)")

Supplied fits contain coordinates and interval bounds, so rendering does not require the original model. A fit name matching an observation group links legend selection. draw_annotate() adds references, bands, and literal labels in the chart’s existing data coordinates. Add these layers before exporting or composing panels.

© 2026 E.D. Gennatas