---
title: "Histograms"
author: "David Gerbing"
output: 
  rmarkdown::html_vignette:
    toc: true
vignette: >
  %\VignetteIndexEntry{2. Visualize: Histogram}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

```{r}
library("lessR")
```



## Data

Most of the examples are an analysis of data in the _Employee_ data set, included with __lessR__. First read the _Employee_ data into the data frame _d_. See the `Read and Write` vignette for more details.

```{r read}
d <- Read("Employee")
```

## Histogram

One of the most frequently encountered visualizations for continuous variables is the histogram, which outlines the general shape the underlying distribution.

>_Histogram_: Bin similar values into a group, then plot the frequency of occurrence of the data values in each bin as the height of the corresponding bar.

A call to a function to create a histogram contains the name of the continuous variable that contains the values to be plotted. With the `Histogram()` function, that variable name is the first argument passed to the function. In this example, the _only_ argument passed to the function is the variable name as the data frame is named _d_, the default value. The following illustrates the call to `Histogram()` with a continuous variable named $x$. 

```{r hsform, dataTable, echo=FALSE, out.width='28%', fig.asp=.7, fig.align='center'}
knitr::include_graphics(system.file("img", "hsExplain.png", package="lessR"))
```

To illustrate, consider the continuous variable _Salary_ in the Employee data table.  Use `Histogram()` to tabulate and display the number of employees in each department, here relying upon the default data frame (table) named _d_ so the `data=` parameter is not needed.

```{r hs, fig.width=4, fig.height=3.5, fig.align='center', fig.cap="Histogram of tablulated counts for the bins of Salary."}
Histogram(Salary)
```

The `Histogram()` function provides a default color theme. The function also provides the corresponding frequency distribution, summary statistics, the table that lists the count of each category, from which the histogram is constructed, as well as an outlier analysis based on Tukey's rules for box plots.

## Customize the Histogram

The parameters `bin_start`, `bin_width`, and `bin_end` are available to customize the histogram.

```{r binwidth, fig.width=4, fig.height=3.5, fig.align='center', fig.cap="Customized histogram."}
Histogram(Salary, bin_start=35000, bin_width=14000)
```

Easy to change the color, either by changing the color theme with `style()`, or just change the fill color with `fill`. Can refer to standard R colors, as shown with **lessR** function `showColors()`, or implicitly invoke the **lessR** color palette generating function `getColors()`. Each 30 degrees of the color wheel is named, such as `"greens"`, `"rusts"`, etc, and implements a sequential color palette. 

Use the `color` parameter to set the border color, here turned off.

```{r colors, fig.width=4, fig.height=3.5, fig.align='center', fig.cap="Customized histogram."}
Histogram(Salary, fill="reds", color="transparent")
```


## Density Plot

The histogram portrays a continuous distribution with discrete bins.

>_Density plot_: A smooth curve that estimates the underlying continuous distribution.  

To invoke, add the `density` parameter. The result is the filled density curve superimposed on the histogram.


```{r density, fig.width=4, fig.height=3.5, fig.align='center', fig.cap="Histogram with density plot."}
Histogram(Salary, density=TRUE)
```


## VBS Plot

A more modern version of the density plot combines the violin plot, box plot, and scatter plot into a single visualization, called here the VBS plot.

```{r VBS, fig.width=4, fig.height=3.5, fig.align='center', fig.cap="VBS plot."}
Plot(Salary)
```



## Full Manual

Use the base R `help()` function to view the full manual for `Histogram()`. Simply enter a question mark followed by the name of the function.

```
?Histogram
```

## More

More on Histograms and other visualizations from __lessR__ and other packages such as __ggplot2__ at:

Gerbing, D., _R Visualizations: Derive Meaning from Data_, CRC Press, May, 2020, ISBN 978-1138599635.
