Learn R Programming

statuser (version 0.1.9)

plot_freq: Plot frequencies of a variable, optionally by group (histogram without binning)

Description

Creates a frequency plot showing the frequency of every observed value, displaying the full range from minimum to maximum value.

Usage

plot_freq(
  formula,
  y = NULL,
  data = NULL,
  labels = NULL,
  freq = TRUE,
  order = NULL,
  col = "dodgerblue",
  lwd = 9,
  width = NULL,
  value.labels = TRUE,
  add = FALSE,
  show.legend = TRUE,
  legend.title = NULL,
  col.text = NULL,
  ...
)

Value

Invisibly returns a data frame with values and their frequencies.

Arguments

formula

A formula of the form x ~ group where x is the variable to plot frequencies for and group is an optional grouping variable (with 2 or 3 unique values). For single variable (no grouping), use x ~ 1. Alternatively, pass a single vector x for a simple frequency plot.

y

An optional second vector to compare with formula. When provided, creates a comparison plot of two variables (like grouped plot but with separate variables). This allows syntax like plot_freq(y1, y2) to compare two vectors.

data

An optional data frame containing the variables in the formula.

labels

An optional character vector of length 2 providing custom labels for the two vectors when using two-vector syntax. Only applicable when y is provided. If NULL (default), uses the variable names. If data is not provided, variables are evaluated from the calling environment.

freq

Logical. If TRUE (default), displays frequencies. If FALSE, displays percentages.

order

Controls the order in which groups appear in the plot and legend. Use -1 to reverse the default order. Alternatively, provide a vector specifying the exact order (e.g., c("B", "A", "C")). If NULL (default), groups are ordered by their factor levels (if the grouping variable is a factor) or sorted alphabetically/numerically. Only applies when using grouped plots or comparing two variables.

col

Color for the bars.

lwd

Line width for the frequency bars. Default is 9.

width

Numeric. Width of the frequency bars. If NULL (default), width is automatically calculated based on the spacing between values.

value.labels

Logical. If TRUE, displays frequencies on top of each line.

add

Logical. If TRUE, adds to an existing plot instead of creating a new one.

show.legend

Logical. If TRUE (default), displays a legend when group is specified. If FALSE, no legend is shown.

legend.title

Character string. Title for the legend when group is specified. If NULL (default), no title is shown.

col.text

Color for the value labels. If not specified, uses col for non-grouped plots or group colors for grouped plots.

...

Pass on any argument accepted by plot() e.g., xlab='x-axis' , main='Distribution of X'

Details

This function creates a frequency plot where each observed value is shown with its frequency. Unlike a standard histogram, there is no binning, unlike a barplot, non-observed values of the variable are shown with 0 frequency instead of skipped.

Examples

Run this code
# Simple example
x <- c(1, 1, 2, 2, 2, 5, 5)
plot_freq(x)

# Pass on some common \code{plot()} arguments
plot_freq(x, col = "steelblue", xlab = "Value", ylab = "Frequency",ylim=c(0,7))

# Add to an existing plot
plot_freq(x, col = "dodgerblue")
plot_freq(x + 1, col = "red", add = TRUE)

# Compare two vectors
y1 <- c(1, 1, 2, 2, 2, 5, 5)
y2 <- c(1, 2, 2, 3, 3, 3)
plot_freq(y1, y2)

# Compare two vectors with custom labels
plot_freq(y1, y2, labels = c("men", "women"))

# Using a data frame with grouping
df <- data.frame(value = c(1, 1, 2, 2, 2, 5, 5), group = c("A", "A", "A", "B", "B", "A", "B"))
plot_freq(value ~ 1, data = df)  # single variable
plot_freq(value ~ group, data = df)  # with grouping

# Control group order in legend and plot
plot_freq(value ~ group, data = df, order = c("B", "A"))  # B first, then A
plot_freq(value ~ group, data = df, order = -1)  # Reverse default order

Run the code above in your browser using DataLab