Skip to main content

7  Scatter plots

library(rtemis.draw)

draw_scatter() draws one point per observation, optionally grouped, sized, and colored by further variables, with fitted curves and their confidence bands. The examples use the built-in penguins dataset.

7.1 Basic

draw_scatter(penguins$bill_len, penguins$flipper_len)

7.2 With Fitted Line

fit names the model fitted to each series and drawn with a 95% confidence band: "glm", "polynomial" (degree fit_params$degree, default 3), "loess" (span fit_params$span, default 0.75), "gam" (mgcv::gam()), or an rtemis learner such as "LINAD". rtemislive’s Fit panel names its fits from the same set.

draw_scatter(
  penguins$bill_len,
  penguins$flipper_len,
  fit = "gam"
)

7.3 Grouped

draw_scatter(
  penguins$bill_len,
  penguins$flipper_len,
  group = penguins$species
)

7.4 Grouped with Fitted Lines

Fit lines and confidence bands are drawn per group and share the group’s color. Clicking a legend entry toggles the scatter points, fit line, and band together:

draw_scatter(
  penguins$bill_len,
  penguins$flipper_len,
  group = penguins$species,
  fit = "gam"
)

7.5 One Fit over Every Group

fit_per_group = FALSE fits one curve over all groups, drawn in gray under its own legend entry. Within each species bill depth rises with bill length; over the three species together it falls, because the species with the longest bills have the shallowest ones:

draw_scatter(
  penguins$bill_len,
  penguins$bill_dep,
  group = penguins$species,
  fit = "glm",
  fit_per_group = FALSE,
  xlab = "Bill length (mm)",
  ylab = "Bill depth (mm)"
)

7.6 Size and Color

size sets each symbol’s area in proportion to its value, the largest value at five times point_size across (30 pixels at the default). color maps values to a continuous color scale, "sequential" from the lowest to the highest value or "diverging" centered on zero (color_scale), which colormap overrides with colors of its own. Each has a key beside the plot, titled by color_title and size_title, on the side legend_position names:

draw_scatter(
  penguins$bill_len,
  penguins$bill_dep,
  size = penguins$body_mass,
  color = penguins$flipper_len,
  color_title = "Flipper (mm)",
  size_title = "Body mass (g)",
  legend_position = "right",
  xlab = "Bill length (mm)",
  ylab = "Bill depth (mm)"
)

7.7 Square Box and Equal Axes

Two arguments control the geometry of the plot rather than its content:

  • square makes the plotting box itself square — the box, excluding axis labels and margins.
  • equal_axes gives one data unit the same size in pixels on both axes.

Set together they also say something about the limits, since the only square box with equal scaling is one whose axes span the same interval: both axes are put on one common interval derived from all the values, or from the one limit given. This is the observed-versus-predicted and ROC case, where the identity line has to run at 45 degrees to be read correctly.

set.seed(2026)
observed <- rnorm(300, mean = 10, sd = 2)
predicted <- observed + rnorm(300, sd = 0.8)

draw_scatter(
  observed,
  predicted,
  square = TRUE,
  equal_axes = TRUE,
  xlab = "Observed",
  ylab = "Predicted",
  title = "Observed vs. Predicted"
)

The box is solved in the browser, which is the only side that knows the container width, and re-solved on every resize — so it stays square as the page reflows. draw_line() takes the same two arguments; an identity line drawn with it lands at exactly 45 degrees:

draw_line(
  c(0, 1),
  c(0, 1),
  square = TRUE,
  equal_axes = TRUE,
  points = FALSE,
  xlab = "False positive rate",
  ylab = "True positive rate"
)

Giving xlim and ylim as different intervals alongside both flags is an error rather than a silent override.

7.8 Configuration

setup_ScatterConfig() describes the same chart by column names; draw() binds it to a data frame, and write_chart_config() saves it as JSON:

config <- setup_ScatterConfig(
  x = "bill_len", y = "bill_dep", group = "species", fit = "glm",
  xlab = "Bill length (mm)", ylab = "Bill depth (mm)"
)
draw(config, data = penguins)
© 2026 E.D. Gennatas