Skip to contents

Visualizes the distribution of a categorical variable using horizontal bar plots of absolute and relative frequencies. Optionally, cumulative proportions (ECDF-style) can be displayed.

Usage

plotCatDist(
  x,
  type = c("both", "freq", "perc"),
  ecdf = FALSE,
  col = "grey80",
  border = FALSE,
  maxlablen = 25,
  maxcats = NULL,
  main = NULL,
  ...
)

Arguments

x

a factor or character vector, a one-dimensional table of counts, or a numeric vector of precomputed frequencies. Unnamed numeric frequency vectors are labelled by their positions.

type

character; one of "both", "freq", "perc". Controls whether absolute frequencies, relative frequencies, or both are displayed.

ecdf

logical; if TRUE, cumulative proportions are shown instead of simple relative frequencies.

col

fill color for bars.

border

logical; draw borders around bars.

maxlablen

integer; maximum length of category labels before truncation.

maxcats

optional maximum number of categories to display (truncates if exceeded).

main

plot title.

...

further graphical parameters passed to par().

Value

Invisibly returns a list with frequencies and proportions.

Details

The function produces horizontal bar plots:

  • Absolute frequencies (counts)

  • Relative frequencies (percentages) or cumulative proportions

If type = "both", both views are shown side by side.

Long labels are truncated, and large category sets can be limited via maxcats.

Raw categorical data and their pre-tabulated form are treated identically. When categories are truncated via maxcats, proportions remain based on the total frequency before truncation.

Examples

# Basic usage
x <- factor(sample(letters[1:5], 100, TRUE))
plotCatDist(x)

plotCatDist(table(x))


# Only proportions
plotCatDist(x, type = "perc")


# With cumulative distribution
plotCatDist(x, ecdf = TRUE)


# Many categories (truncation)
x2 <- factor(sample(letters, 200, TRUE))
plotCatDist(x2, maxcats = 10)