# Plotting histograms


Find this notebook on the web at
<a class="quarto-xref" href="https://resampling-stats.github.io/latest-r/probability_theory_3.html#nte-on_histograms">Note <span>12.6</span></a>.

A histogram is a visual way to show the *distribution* of a sequence of
values.



The easiest way to explain histograms is to show one.

Let’s start with a sequence of values we are interested in:

Here are the 24 values for whiskey prices in states that did not have a
liquor monopoly (`priv`).

```{r}
priv <- c(4.82, 5.29, 4.89, 4.95, 4.55, 4.90, 5.25, 5.30, 4.29, 4.85, 4.54,
          4.75, 4.85, 4.85, 4.50, 4.75, 4.79, 4.85, 4.79, 4.95, 4.95, 4.75,
          5.20, 5.10, 4.80, 4.29)
```

These are the 16 values for states with a liquor monopoly (`govt`):

```{r}
govt <- c(4.65, 4.55, 4.11, 4.15, 4.20, 4.55, 3.80, 4.00, 4.19, 4.75, 4.74,
          4.50, 4.10, 4.00, 5.05, 4.20)
```

We concatenate these values to get a sequence (a vector) of all 40
liquor prices:

```{r}
prices <- c(priv, govt)
prices
```

We are interested in the distribution of these 40 values. To show the
distribution, we can make and show a histogram of these prices, using
the `hist` function in R.

```{r}
hist_res <- hist(prices)
```

`hist` has calculated a vector of suitable intervals (*bins*) to divide
up the range of values, and then counted how many values in `prices`
fall into each interval (bin).

You will notice that `hist` has sent back some results from the process
of making the histogram.

The first result of interest to us is the definition of the intervals
(bins) into which the histogram has divided the range of `prices`
values.



In fact, `hist` sent back the edges (or *breaks*) of these bins as part
of the `hist_res` results. `hist_res` is a special R value to contain
the results of the histogram. It contains various relevant values.

```{r}
hist_res
```

Notice that the values attached to the `hist_res` result have *names*,
such as `breaks`, `counts` and so on. To get the attached values from
the `hist_res` results, we use the `$` character to mean *attached to*.
So read:

```{r}
hist_res$counts
```

as “get the value called `counts` that is attached to the `hist_res`
value.”

When we called `hist(prices)`, the `hist` function had to decide how
many bins to use, and the edges of the bins. By default, it does this
with an algorithm called [Sturge’s
rule](https://en.wikipedia.org/wiki/Sturges%27s_rule). The details of
the algorithm are not important to us, but the result is that `hist`
decided to make 9 bins, with the edges you see in the `breaks` value of
`hist_res`.

```{r}
# Bin edges
hist_res$breaks
```

Think of the `breaks` values as: the value that starts the first bin,
followed by the 9 values that end each of the 9 bins.

The `breaks` vector shows that the first bin was from (including) 3.8 up
to, *and including* 4.0, the second bin was from (not including) 4.0 up
to, and including 4.2 and so on. The last bin is from (not including)
5.2 through (including) 5.4. There are 10 edges (breaks), forming 9
bins.[^1]

Put another way, the edges (boundaries) that `hist` sent back start with
the left-hand (inclusive) edge of the first bin, followed by the 9
right-hand (inclusive) edges of the 9 bins.

The list of results in `hist_res` includes the vector of *counts* of
values in `prices` that fall within each bin.

```{r}
# Counts of values falling into each bin.
hist_res$counts
```

The `counts` vector shows that 3 values from `prices` fell in the range
3.8 through 4.0, 6 values fell in the range from (not including) 4.0
through 4.2, and so on.

The counts correspond to the heights of the bars on the histogram, so
the first bar has height 3, the second bar has height 6, and so on.

By default, `hist` uses Sturge’s rule to decide on the number of bins.
You can also *suggest* to R that it uses another number of bins, by
sending a number for the `breaks` argument to `hist`. For example, you
might want to suggest 20 bins:

```{r}
results_20 <- hist(prices, breaks=20)
```

Notice that R treats this number as a suggestion, and will adjust the
actual number of bins to something close to the suggestion and that
result in suitably rounded bin edges. In fact, R responded to the
suggestion of 20 by giving 15 bins:

```{r}
length(results_20$counts)
```

We now have 16 new edges (boundaries), the first value giving the
(inclusive) left-hand edge of the first bin, and the subsequent 15
values giving the (inclusive) right-hand edges.

```{r}
results_20$breaks
```

We can also specify our own `breaks`, in order to bypass `hist`s default
algorithm. For example, we might prefer exactly 16 bins of size 0.1,
with edges starting at 3.8:

```{r}
our_edges <- 3.8 + 0:15 * 0.1
our_edges
```

We can send these directly to `hist` to specify the edges (and the
number of bins):

```{r}
results_16 = hist(prices, breaks=our_edges)
```

```{r}
# Show the edges that come back (these are the edges we sent).
results_16$breaks
```



[^1]: You can customize the calculation and meaning of the `breaks`
    values using various arguments to the `hist` function. See the
    documentation of `hist` for more details.
