histlines: Utility to create line-based histogram

View source: R/histlines.R

histlinesR Documentation

Utility to create line-based histogram


Utility function to plot histogram with graphics::lines().


histlines(x, y = NULL, breaks, use = c("counts", "density"))



Either vector of breaks or the data itself.


Optional vector of density/counts, with length = length(x)-1.


Breaks for histogram, if y is not provided.


Whether to use counts or density, if y is not provided.


If x and y are both provided, x is interpreted to be the breaks for a histogram, and y is a vector of counts or density values for each interval. These are then revised so that they may be plotted with graphics::lines(). If y is NULL, x is taken to be the data. In this case graphics::hist() is called with breaks=breaks, and either the counts or density are used as y.


A data.frame with two columns: x and y.

See Also

graphics::hist(), graphics::lines()


x <- rnorm(1000, mean=20, sd=5)
# basic use
out <- hist(x, breaks=60, plot=FALSE)
plot(histlines(out$breaks, out$counts),
     type="l", lwd=2, xlab="x", ylab="counts", las=1)
# alternative use
plot(histlines(x, breaks=60, use="density"),
     type="l", lwd=2, xlab="x", ylab="Density", las=1)
# comparing two distributions
z <- rnorm(1000, mean=25, sd=5)
br <- seq(min(c(x,z)), max(c(x,z)), len=50)
xlines <- histlines(x, breaks=br, use="density")
zlines <- histlines(z, breaks=br, use="density")
ymx <- max(c(xlines$y, zlines$y))*1.05
plot(xlines, ylim=c(0, ymx), yaxs="i", xaxs="i",
     type="l", lwd=2, xlab="x", ylab="Density", las=1,
lines(zlines, lwd=2 , col="red")

broman documentation built on July 8, 2022, 5:07 p.m.

Related to histlines in broman...