Skip to contents

Create scatter plots with regression lines and confidence intervals using Insper's visual identity. Supports both color and fill aesthetics for maximum flexibility with different point shapes.

Usage

insper_scatterplot(
  data,
  x,
  y,
  color = NULL,
  fill = NULL,
  palette = "categorical",
  add_smooth = FALSE,
  smooth_method = "lm",
  point_size = 2,
  point_alpha = 1,
  ...
)

Arguments

data

A data frame containing the data to plot

x

Variable for x-axis

y

Variable for y-axis

color

Color aesthetic. Accepts either:

  • A bare column name for variable mapping (e.g., color = Species)

  • A quoted color string for static color (e.g., color = "blue")

  • NULL (default) to use default Insper teal

When mapping a variable, the appropriate scale is automatically applied.

fill

Fill aesthetic (for shapes 21-25 with fill interiors). Accepts either:

  • A bare column name for variable mapping (e.g., fill = Species)

  • A quoted color string for static fill (e.g., fill = "lightblue")

  • NULL (default) - no fill mapping

palette

Character. Color palette for variable mappings. Default is "categorical".

add_smooth

Logical. If TRUE, adds a regression line. Default is FALSE

smooth_method

Character. Smoothing method ("lm", "loess", "gam", "glm"). Default is "lm"

point_size

Numeric. Size of points. Default is 2

point_alpha

Numeric. Transparency of points (0-1). Default is 1

...

Additional arguments passed to ggplot2::geom_point(), allowing custom aesthetics like shape, stroke, etc.

Value

A ggplot2 object

Details

This function supports two types of point shapes:

  • **Solid shapes (16-20)**: Only use color aesthetic for point color

  • **Outlined shapes (21-25)**: Use both color (outline) and fill (interior)

For outlined shapes, you can map different variables to color and fill, or use static colors for fine-grained control.

Examples

if (FALSE) { # has_insper_fonts()
# Simple scatter plot with default color
insper_scatterplot(mtcars, x = wt, y = mpg)

# Discrete variable mapping
insper_scatterplot(mtcars, x = wt, y = mpg, color = factor(cyl))

# With smooth line
insper_scatterplot(mtcars, x = wt, y = mpg, add_smooth = TRUE)

# ... arguments always passed to geom_point()
insper_scatterplot(mtcars, x = wt, y = mpg, size = 3, alpha = 0.5)

# Shape 21 with static color and mapped fill
insper_scatterplot(mtcars, x = wt, y = mpg,
                   color = "white",
                   fill = factor(cyl),
                   shape = 21)
}