The Bar Chart

For a general presentation of using R and lessR, including examples of bar charts, access the following sources.

Categorical Variables

A categorical variable has a relatively small number of unique values, called levels. For example, on a survey form, Gender can be assessed with three levels: Male, Female, and Other. Or, these values can be represented as integers: 1, 2, and 3. Wether the values are of type character, the R variable type for a categorical variable, type factor, or type integer, the corresponding variable Gender is categorical.

The bar chart associates a number with each level of a categorical variable. A bar chart is constructed from this table with two columns, one column a level of the categorical variable and the second column the associated number. The chart consists of a bar for each level with a height proportional to that associated number. In general, the associated number can be any value but usually is the result of a statistical calculation.

Types of Bar Charts

There are three possibilities for the source of the number associated with each level or category of the categorical variable of interest.

Count the Values for Each Level

Probably the most common type of bar chart calculates the number associated with each level as the count of the number of values for the corresponding level of the categorical variable. This bar chart answers the question: How often did the values of each level of the categorical variable occur in the data? For example, how many students in the classroom term for homework assignment in on time, and how many turned in the assignment late? The only needed information for which to compute this type of bar chart are the values of the categorical variable.

Calculate a Statistic for a Numerical Variable

The number associated with each category or level of a categorical variable can also be computed as a statistic from the analysis of a numerical variable at each level or category. For example, calculate the average salary for for the people each department of a company, such as accounting, sales, etc.

Read the Summary Table Directly

The possibility of reading a numerical variable also allows for the possibility reading the summary table from which the bar chart is constructed as the data for the analysis. As discussed, every bar chart is constructed from a summary table that associates each level of the categorical terrible with a number. Usually that number is calculated as either the counted values of a categorical variable for each level, or the calculation of a statistic for a numerical variable. Or, just enter that summary table directly as the given data, select the categorical variable, then select the corresponding numerical variable. This way the numerical variable can be anything at all, including constructing a bar chart for a published table without having access to the original data.

The Two-Variable Bar Chart

The two variable bar chart visualizes the relationship between two categorical variables. The corresponding cross-tabulation table shows that same relationship in tabular form from which the bar chart visualization is derived.

Palettes Addressed to Color-Blindness

Typical color palettes are not designed to account for color-blindness. Two palettes included in lessR do permit more sensitivity to differentiate among objects with various types of color blindness: Okabe-Ito and viridis. These are qualitative palettes, which means that the hue varies across the plotted objects, here bars. Specify these, or other palettes, as the value for the fill parameter, available both by entering a function call in the R console or via the interactive analysis.

Parameters

For the full list of BarChart() parameters, see the manual obtained by entering ?BarChart. These listed parameters are those provided in the interactive session from interact("BarChart").

x-variable

Categorical variable for which each level is associated with a number, proportional to the height of the corresponding bar on the bar chart.

by-variable

Optional categorical variable to create a two-variable a bar chart.
fill
interior color of a bar for each level of the by-variable
beside
define a separate, adjacent bar for each level of the by-variable within the corresponding level of the x-variable
stack100
create a 100% stacked bar chart in which each bar shows the relative percentage of items within each level or category

y-variable

optional numerical variable to compute a statistic across the levels of the categorical variable, such as the mean
stat
statistic computed across the levels of the categorical variable from which to compute the height of the bars

Bars

fill
interior color of a bar
color
exterior color of a bar, i.e., its border
transparency
transparency level of 0 (none) to 1 (completely transparent)
sort
sort the levels of the x-variable, "0" for none, "+" for ascending, and "-" for descending
horiz
display the bars horizontal

Values

values
type of value displayed, "%" for percent, "input" for entered data values, "off" for none
values_color
color of displayed values
values_position
position of displayed values, "in" the object or "out", on top
values_size
size of the displayed values

Save

width
width of saved pdf file, in inches
height
height of saved pdf file, in inches