Histograms

suppressPackageStartupMessages(library("lessR"))

Data

Most of the following 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.

d <- Read("Employee")

As an option, also read the table of variable labels. Create the table formatted as two columns. The first column is the variable name and the second column is the corresponding variable label. Not all variables need be entered into the table. The table can be a csv file or an Excel file.

Read the label file into the l data frame, currently the only permitted name. The labels will be displayed on both the text and visualization output. Each displayed label is the variable name juxtaposed with the corresponding label, as shown in the following output.

l <- rd("Employee_lbl")
l

Histogram

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

Histogram: Bin similar values into a group, then plot the frequency of occurrence of the data values in each bin proportional to 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 plotted values. With the Histogram() function, that variable name is the first parameter value passed to the function. In this example, the only parameter value passed to the function is the variable name. The data frame is named d, the default value. The following illustrates the call to Histogram() with a continuous variable named $x$.

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.

Histogram(Salary)

By default, the Histogram() function provides a color theme according to the current, active 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 outlier detection rules for box plots.

Customize the Histogram

Use the parameters bin_start, bin_width, and bin_end to customize the 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.

Histogram(Salary, fill="reds", color="transparent")

Density Plot

The histogram portrays a continuous distribution with discrete bins, with more modern visualizations available that directly display the estimated underlying smooth curve.

Density plot: A smooth curve that estimates the underlying continuous distribution.

To create a density plot, add the density parameter. The result is the filled density curve superimposed on the histogram.

Histogram(Salary, density=TRUE)

The type parameter indicates the type of density curve. The default is "general". Options are "normal" for a normal density curve and "both" for both.

VBS Plot

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

Plot(Salary)

Interactive Histogram

An interactive visualization lets the user in real time change parameter values to change characteristics of the visualization. To create an interactive histogram of the variable Salary that displays the corresponding parameters, run the function interact() with "Histogram" specified.

interact("Histogram")

The interact() function is not run here because interactivity requires to run directly from the R console.

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.



Try the lessR package in your browser

Any scripts or data that you put into this service are public.

lessR documentation built on Nov. 12, 2023, 1:08 a.m.