bcat_plt_point: Scatter plot utility

View source: R/bcat_plt_point.R

bcat_plt_pointR Documentation

Scatter plot utility

Description

Create a scatter plot using ggplot2 graphics. This function is a wrapper to create commonly used styles of scatter plots. Additional layers can be added to this plot as needed. More complicated scatter plots can be creating using individual ggplot2 layers.

Usage

bcat_plt_point(
  df,
  x,
  y,
  color = NULL,
  size = NULL,
  facet = NULL,
  jitter = FALSE,
  jitter_width = NULL,
  smooth = FALSE,
  method = "lm",
  se = TRUE,
  x_lab = ggplot2::waiver(),
  y_lab = ggplot2::waiver(),
  title = ggplot2::waiver(),
  subtitle = ggplot2::waiver(),
  caption = ggplot2::waiver(),
  legend_lab = ggplot2::waiver(),
  legend_position = "bottom",
  legend_hide = FALSE,
  x_scale = NULL,
  y_scale = NULL,
  color_scale = Rbearcat::scale_colour_UC(),
  fill_scale = Rbearcat::scale_fill_UC(),
  facet_scale = c("fixed", "free_y", "free_x", "free"),
  alpha = 0.6,
  nrow = NULL,
  ncol = NULL,
  x_refline = NULL,
  y_refline = NULL,
  identity_line = FALSE,
  x_highlight_min = NULL,
  x_highlight_max = NULL,
  y_highlight_min = NULL,
  y_highlight_max = NULL,
  y_error_min = NULL,
  y_error_max = NULL,
  y_error_width = 1
)

Arguments

df

The data to be displayed

x

Variable to map to the x-axis

y

Variable to map to the y-axis

color

Variable to map to the color aesthetic

size

Variable to map to the size aesthetic

facet

Facetting variable(s). Note: must wrap in vars(), e.g, facet = vars(var1, var2)

jitter

Set to TRUE to enable jittering.

jitter_width

Set the jitter width. Leave as NULL to use the default ggplot2 settings

smooth

Add fit line to plot

method

Method to use for fit line. "lm" is default

se

Set to FALSE to remove standard error band around fit line

x_lab

Label for x-axis

y_lab

Label for y-axis

title

Plot title

subtitle

Plot subtitle

caption

Plot caption

legend_lab

Legend title

legend_position

legend position. "bottom" or "right"

legend_hide

Set to TRUE to hide the legend

x_scale

scale_x_ function to apply to x-axis.

y_scale

scale_y_ function to apply to y-axis.

color_scale

scale_color_ function to apply to colors.

fill_scale

scale_fill_ function to apply to colors. only applicable if smooth = TRUE'

facet_scale

Shoud facet scales be fixed ("fixed", the default), free ("free"), or free in one dimension ("free_x", "free_y")?

alpha

level of point transparency. lower alpha leads to more transparency.

nrow

Number of facet rows

ncol

Number of facet columns

x_refline

Vector of x-values at which to draw vertical reference lines

y_refline

Vector of y-values at which to draw horizontal reference lines

identity_line

Set to TRUE to draw 45 degree identity line

x_highlight_min

Vector of min x-values at which to start draw rectangle annotation

x_highlight_max

Vector of max x-values at which to end rectangle annotation

y_highlight_min

Vector of min y-values at which to start draw rectangle annotation

y_highlight_max

Vector of max y-values at which to end rectangle annotation

y_error_min

Variable to use as minimum values for error bars around y at each value of x

y_error_max

Variable to use as maximum values for error bars around y at each value of x

y_error_width

Width of error bars

Value

A ggplot2 plot object.

Author(s)

Saannidhya Rawat

See Also

Other plots: bcat_plt_area(), bcat_plt_bar(), bcat_plt_box(), bcat_plt_coef(), bcat_plt_diag(), bcat_plt_hist(), bcat_plt_line(), bcat_plt_ts()

Examples

library(ggplot2)

# basic scatter plot
bcat_plt_point(df = iris,
               x = Sepal.Length,
               y = Sepal.Width,
               x_lab = "Length",
               y_lab = "Width",
               title = "Sepal Width vs Length")

# scatter plot with LOESS fit line
bcat_plt_point(df = iris,
               x = Sepal.Length,
               y = Sepal.Width,
               x_lab = "Length",
               y_lab = "Width",
               smooth = TRUE,
               method = "loess",
               title = "Sepal Width vs Length",
               subtitle = "Loess Fit")

 # scatter plot with faceting and LM fit lines
 bcat_plt_point(df = iris,
                x = Sepal.Length,
                y = Sepal.Width,
                color = Species,
                facet = vars(Species),
                smooth = TRUE,
                x_lab = "Length",
                y_lab = "Width",
                legend_lab = NULL,
                title = "Sepal Width vs Length by Species",
                subtitle = "Linear Fit",
                nrow = 1)

Rbearcat documentation built on March 21, 2026, 5:07 p.m.