Skip to contents

Quick scatter plot from x/y data with optional fitted line and confidence band.

Usage

draw_scatter(
  x,
  y,
  size = NULL,
  group = NULL,
  fit = NULL,
  fit_params = NULL,
  se = TRUE,
  fit_alpha = 0.25,
  n_fit = 200L,
  palette = NULL,
  xlim = NULL,
  ylim = NULL,
  pad = DEFAULT_PAD,
  square = FALSE,
  equal_axes = FALSE,
  xlab = NULL,
  ylab = NULL,
  title = NULL,
  theme = NULL,
  margins = DEFAULT_MARGINS,
  width = NULL,
  height = NULL,
  element_id = NULL,
  filename = NULL,
  se_times = 1.96,
  rsq = FALSE,
  diagonal = FALSE,
  diagonal_color = NULL,
  legend_position = "top",
  legend_placement = "outside",
  legend_title = NULL,
  fit_name = NULL,
  rug = FALSE,
  hover = NULL,
  point_alpha = NULL,
  zoom = FALSE
)

Arguments

x

Numeric: X values.

y

Numeric: Y values.

size

Optional Numeric: Symbol sizes.

group

Optional Atomic vector or single-column data frame: Grouping variable for multiple series. Series, colors, and legend entries follow factor level order, or first appearance for other vectors.

fit

Optional Character: Model fitted to y on x and drawn as a line. "glm" uses stats::glm() and "gam" uses mgcv::gam(). Any other name, matched case-insensitively, is an rtemis supervised learning algorithm, such as "LINAD", "CART" or "LightGBM", trained with its default hyperparameters; this requires the rtemis package (>= 1.4.1). NULL disables fitting. The fitted line and standard-error band are computed per group when group is provided.

fit_params

Optional Named list: Arguments passed to the learner named in fit, as argument-value pairs: to stats::glm() for "glm", e.g. list(family = "poisson"); to mgcv::gam() for "gam", where k sets the basis dimension of the smooth, e.g. list(k = 5); and to the rtemis setup function otherwise, e.g. list(max_leaves = 8) for "LINAD", whose arguments are listed in ?rtemis::setup_LINAD. NULL uses the learner's defaults.

se

Logical: Whether to show the standard-error band. Algorithms without standard errors (every rtemis algorithm except GLM, GAM and a few others) draw the line alone.

fit_alpha

Numeric [0, 1]: Opacity for the confidence-band fill.

n_fit

Integer [2, Inf): Number of evaluation points for the fit.

palette

Optional Character: Series color palette — a single color string or character vector that overrides the theme palette for this chart. When group is set, colors are assigned per group in order. color takes precedence over the theme palette.

xlim

Optional Numeric [length 2]: X-axis limits c(min, max). Defaults to range(x) extended by pad.

ylim

Optional Numeric [length 2]: Y-axis limits c(min, max). Defaults to range(y) extended by pad.

pad

Numeric [0, Inf): Fraction of the data range to extend each axis by when xlim / ylim are not given. The default matches base R's xaxs = "r", which extends the range by 4% at each end.

square

Logical: If TRUE, draw the plotting box square – equal height and width in pixels, excluding axis labels and margins.

equal_axes

Logical: If TRUE, give one data unit the same size in pixels on both axes. Set both for a plot that is square and to scale, such as a true-versus-predicted plot whose identity line should run at 45 degrees; the two axes are then made to span the same interval. See Details.

xlab

Optional Character: X-axis title.

ylab

Optional Character: Y-axis title.

title

Optional Character: Chart title.

theme

Optional Theme: Theme override. The palette inside the theme can be overridden per-chart with the color argument.

margins

Optional Named numeric vector or named list: Plot margins in pixels (or percentage strings) for any of "top", "right", "bottom", "left". Unspecified sides keep echarts' default auto-sizing. See draw_line() for details.

width

Optional Character or Numeric: Widget width.

height

Optional Character or Numeric: Widget height.

element_id

Optional Character: Explicit DOM element ID for the widget container. NULL lets htmlwidgets generate one.

filename

Optional Character: If provided, save the widget to this file via save_drawing().

se_times

Numeric [0, Inf): Standard-error multiplier for the band.

rsq

Logical: Include the fitted model's R-squared in series labels. This describes the overlay fit, not predictive performance against the identity line. Constant responses have undefined R-squared, labeled NA.

diagonal

Logical: Draw a dashed identity line within the axis limits.

diagonal_color

Optional Character: Identity-line color.

legend_position

Character {"top", "bottom", "left", "right", "top-left", "top-right", "bottom-left", "bottom-right"}: Legend anchor. Top/bottom anchors use horizontal rows; left/right anchors use a vertical column. Corner anchors align within the top or bottom row.

legend_placement

Character {"outside", "inside"}: Relation to the plotting area. Outside placement reserves space for the complete legend; inside placement overlays the data. Neither setting adds a missing legend.

legend_title

Optional Character: Title drawn with the legend or colorbar: above left/right legends and vertical colorbars, before the first row of top/bottom legends and horizontal colorbars. NULL draws no title. A title does not add a missing legend.

fit_name

Optional Character: Label for fitted layers.

rug

Logical: Draw marginal marks on both axes.

hover

Optional Character: One literal tooltip label per observation.

point_alpha

Optional Numeric [0, 1]: Point opacity. NULL sets it from the number of points drawn, as a rough guard against overplotting: 0.75 at 10 points, 0.57 at 150, 0.45 at 1,000 and 0.3 at 10,000, clamped to [0.15, 0.9]. Set a value to override.

zoom

Logical, DataZoom, or list of DataZoom: Enable zoom on both axes. TRUE adds Shift+wheel zoom and drag panning on both axes and a top-right toolbox with box-zoom, undo, and reset controls; double-click resets the view. Wheel zoom and panning scale both axes by the same factor; with square or equal_axes, the toolbox omits box zoom, which sets each axis independently. FALSE disables zoom. Pass DataZoom objects (or a list of them) for full control.

Value

htmlwidget: Widget object.

Square and equally-scaled plots

square and equal_axes are separate requests. square is about the shape of the plotting box: equal height and width in pixels, measured on the box itself, not including axis labels or margins. equal_axes is about scale: one data unit occupies the same number of pixels on both axes, so a 45-degree line really is a slope of 1.

Either works alone. equal_axes on a chart whose x spans ten units and whose y spans one gives a wide, short box, which is what equal scaling means there.

Asking for both is a statement about the limits, since the only square box in which both axes are equally scaled is one whose axes span the same interval. So both axes are made to span one interval: over all the values when neither limit was given, or the one you gave when you gave exactly one. Giving both xlim and ylim as different intervals is an error rather than a silent override.

The box itself is solved in the browser, which is the only side that knows how wide the container is, and re-solved whenever it resizes.

Examples

# True versus predicted: square box, equal scale, identity line at 45 deg.
draw_scatter(
  x = c(1, 2, 3, 4),
  y = c(1.1, 1.9, 3.4, 3.7),
  square = TRUE,
  equal_axes = TRUE,
  xlab = "True",
  ylab = "Predicted"
)