Description Usage Arguments Details Value Examples
Similar to base R function hist
, but with two added
features: (1) Can overlay a fitted probability density/mass function
(PDF/PMF) for any univariate distribution supported in R (see
Distributions
); and (2) Can generate more of a barplot
type histogram, where each possible value gets its own bin centered over its
value (useful for discrete variables with not too many possible values).
1 2 |
x |
Numeric vector of values. |
dis |
Character vector indicating which distribution should be used to
add fitted PDF/PMF to the histogram. Possible values are |
dis.shift |
Numeric value for shifting the fitted PDF/PMF along the x-axis of the histogram. |
integer.breaks |
If |
points.list |
Optional list of inputs to pass to
|
axis.list |
Optional list of inputs to pass to
|
... |
May include arguments to pass to |
When x
takes on whole numbers, you typically want to set
dis.shift = -0.5
if right = TRUE
(hist
's default) and dis.shift = 0.5
if
right = FALSE
. The function will do this internally by default.
To illustrate, suppose a particular bin represents (7, 10]
. Its
midpoint will be at x = 8.5
on the graph. But if input values are
whole numbers, this bin really only includes values of 8, 9, and 10, which
have a mean of 9. So you really want f(9)
to appear at x = 8.5
.
This requires shifting the curve to the left 0.5 units, i.e. setting
dis.shift = -0.5
.
When x
takes on whole numbers with not too many unique values, you may
want the histogram to show one bin for each integer. You can do this by
setting integer.breaks = TRUE
. By default, the function sets
integer.breaks = TRUE
if x
contains whole numbers with 10 or
fewer unique values.
Histogram with fitted PDF/PMF if requested.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | # Generate 10,000 Poisson(2) values. Compare default histograms from hist vs.
# histo.
set.seed(123)
x <- rpois(n = 10000, lambda = 2)
par(mfrow = c(1, 2))
hist(x)
histo(x)
# Generate 10,000 lognormal(0, 0.35) values. Create histograms with curves
# showing fitted log-normal and normal PDFs.
set.seed(123)
x <- rlnorm(n = 10000, meanlog = 0, sdlog = 0.35)
par(mfrow = c(1, 2))
histo(x, "lnorm", main = "Log-normal curve")
histo(x, "norm", main = "Normal curve")
# Generate 10,000 Binomial(8, 0.25) values. Create histogram, specifying
# size = 5, with blue line/points showing fitted PMF.
set.seed(123)
x <- rbinom(n = 10000, size = 5, prob = 0.25)
par(mfrow = c(1, 1))
histo(x, "binom", size = 5, points.list = list(type = "b", col = "blue"))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.