ghist: Histogram

View source: R/zzz-r4vn-graphs.R View source: R/graphs.R

ghistR Documentation

Histogram

Description

Draws histograms for one or several numeric variables using base R graphics. by may be a single grouping variable or hierarchical vars(...); histogram panels are created for observed group/stratum combinations.

Usage

ghist(
  data = NULL, x = NULL, vars = NULL, by = NULL, bins = "Sturges",
  density = FALSE, normal = FALSE, xlab = NULL, ylab = NULL, xtitle = NULL,
  ytitle = NULL, xbreaks = NULL, ybreaks = NULL, title = NULL,
  subtitle = NULL, note = NULL, color = NULL, palette = "default",
  alpha = 0.85, border_color = "white", normal_color = "black",
  normal_lty = 1, normal_lwd = 2, xline = NULL, yline = NULL,
  ref_color = "gray40", ref_lty = 2, ref_lwd = 1, theme = "journal",
  size = 11, combine = FALSE, ncol = NULL, file = NULL, width = 7, height = 5,
  dpi = 300, show = TRUE, bg = "white", vline = NULL, hline = NULL
)

Arguments

data

A data frame. It may be omitted when an active R4VN data frame exists.

x

Categorical variable on the horizontal axis. Supply an unquoted name or a character name.

vars

Optional vars(...) selector used to request several variables in one call.

by

Optional grouping variable or hierarchical vars(...) specification.

bins

Number of bins, a vector of break points, or a valid value for graphics::hist().

density

If TRUE, the vertical axis shows density rather than counts.

normal

Add a fitted normal density curve.

xlab

Optional category labels. Use an unnamed vector in displayed order or a named vector such as c(M = "Male", F = "Female").

ylab

Optional y-axis tick labels.

xtitle, ytitle

Axis titles. Variable names or variable labels are used automatically when possible.

xbreaks

Numeric x-axis tick positions used with xlab.

ybreaks

Numeric positions used with ylab.

title, subtitle, note

Main title, subtitle, and note.

color

A color name or vector of colors. When NULL, palette is used.

palette

One of "default", "journal", "blue", "green", "warm", "gray", or "colourblind"; alternatively, a color vector.

alpha

Color opacity from 0 to 1.

border_color

Histogram-bar border colour.

normal_color, normal_lty, normal_lwd

Colour, line type, and width of the optional normal-reference curve.

xline, yline

Optional numeric reference lines on the x and y axes. Thus xline draws vertical lines and yline draws horizontal lines.

ref_color, ref_lty, ref_lwd

Color, line type, and width for reference lines; vectors are recycled for multiple lines.

theme

Graph theme: "journal", "clean", "minimal", or "classic".

size

Base font size.

combine

When several graphs are produced, draw them as labelled panels in one figure.

ncol

Optional number of columns in a combined figure.

file

Optional output file ending in png, jpg, tiff, pdf, or svg.

width, height

Output width and height in inches.

dpi

Resolution for raster output.

show

Draw the graph in the current graphics device.

bg

Background color for exported files.

vline, hline

Deprecated aliases for xline and yline.

Value

An object of class r4vn_graph; its data component contains histogram breaks, counts, density, and midpoints.

Examples

d <- data.frame(age = c(18, 21, 25, 27, 31, 34, 38, 45, 51, 62))
ghist(d, x = age)
ghist(d, x = age, bins = 5, normal = TRUE, color = "steelblue")

d$sex <- rep(c("Female", "Male"), length.out = nrow(d))
ghist(d, x = age, by = sex, normal = TRUE, xline = 40, ref_lty = 2)
ghist(d, vars = vars(age), by = vars(sex), normal = TRUE, combine = TRUE)


# Extended usage examples

d <- data.frame(age = c(18, 21, 25, 27, 31, 34, 38, 45, 51, 62))
ghist(d, x = age, show = FALSE)
ghist(d, x = age, bins = 5, show = FALSE)
ghist(d, x = age, density = TRUE, normal = TRUE, show = FALSE)


R4VN documentation built on Sept. 30, 2026, 5:13 p.m.