# 2. Visualize: Charts

#### David Gerbing

## Data

### Read

Most of the following examples analyze data in the Employee data set, included with lessR. To read an internal lessR data set, just pass the name of the data set to the lessR function `Read()`. Read the Employee data into the data frame d. See the `Read and Write` vignette for more details.

    d <- Read("Employee")

    ##
    ## >>> Suggestions
    ## Recommended binary format for data files: feather
    ##   Create with Write(d, "your_file", format="feather")
    ## More details about your data, Enter:  details()  for d, or  details(name)
    ##
    ## Data Types
    ## ------------------------------------------------------------
    ## character: Non-numeric data values
    ## integer: Numeric data values, integers only
    ## double: Numeric data values with decimal digits
    ## ------------------------------------------------------------
    ##
    ##     Variable                  Missing  Unique
    ##         Name     Type  Values  Values  Values   First and last values
    ## ------------------------------------------------------------------------------------------
    ##  1     Years   integer     36       1      16   7  NA  7 ... 1  2  10
    ##  2    Gender character     37       0       2   M  M  W ... W  W  M
    ##  3      Dept character     36       1       5   ADMN  SALE  FINC ... MKTG  SALE  FINC
    ##  4    Salary    double     37       0      37   63788.26  104494.58 ... 66508.32  67562.36
    ##  5    JobSat character     35       2       3   med  low  high ... high  low  high
    ##  6      Plan   integer     37       0       3   1  1  2 ... 2  2  1
    ##  7       Pre   integer     37       0      27   82  62  90 ... 83  59  80
    ##  8      Post   integer     37       0      22   92  74  86 ... 90  71  87
    ## ------------------------------------------------------------------------------------------

## Chart of One Variable

### Bar Chart

One of the most frequently encountered visualizations is the bar chart, created for the values of a categorical variable that are each associated with a corresponding value of a numerical variable.

> Bar chart: Plot a bar for each level of a categorical variable with its height scaled according to the value of an associated numerical variable.

A call to the chart function contains, at a minimum, the name of the categorical variable with the categories to be plotted. With the `Chart()` 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 lessR default value. Or, specify the data frame that contains the variable(s) of interest with the `data` parameter.

The following illustrates the call to `Chart()` with a categorical variable named \(x\).

If only a single categorical variable is passed to `Chart()`, the numerical value associated with each bar is the corresponding count of the number of occurrences, automatically computed.

Consider the categorical variable Dept in the Employee data table, with one of five departments recorded for each employee. Use `Chart()` to tabulate and display the visualization of the number of employees in each department, here relying upon the default data frame (table) named d. Otherwise add the `data=` option for a data frame with another name.

    Chart(Dept)

    ## [Interactive chart from the Plotly R package (Sievert, 2020)]
    ##
    ## >>> Suggestions  or  enter: style(suggest=FALSE)
    ## Chart(Dept, type="radar")  # Plotly radar chart
    ## Chart(Dept, type="treemap")  # Plotly treemap chart
    ## Chart(Dept, type="pie")  # Plotly pie/sunburst chart
    ## Chart(Dept, type="icicle")  # Plotly icicle chart
    ## Chart(Dept, type="bubble")  # Plotly bubble chart
    ##

Bar chart of tablulated counts of employees in each department.

    ## --- Dept ---
    ##
    ## Missing Values: 0
    ##
    ##                 ACCT   ADMN   FINC   MKTG   SALE    Total
    ## Frequencies:       5      6      4      6     15       36
    ## Proportions:   0.139  0.167  0.111  0.167  0.417    1.000
    ##
    ## Chi-squared test of null hypothesis of equal probabilities
    ##   Chisq = 10.944, df = 4, p-value = 0.027

The default color theme, `"colors"`, fills the bars in the bar chart with with different hues. See more explanation of this and related color palettes in the vignette Customize.

`Chart()` also labels each bar with the associated numerical value. The function provides the corresponding frequency distribution, the table that lists the count of each category, from which the bar chart is constructed.

For more examples, we do not need to see this output to the R console repeated again for different charts of the same data, so turn off for now with the parameter `quiet` set to `TRUE`. You can set this option for each call to `Chart()`, or you can set as the default for subsequent analyses with the `style()` function.

Following are other chart possibilities. Indicate a specific form with the parameter `type`. The default is `type="bar"` for a bar chart.

### Pie Chart

An alternative to the bar chart for a single categorical variable is the pie chart.

> Pie Chart: Relate each level of a categorical variable to the area of a circle (pie) scaled according to the value of an associated numerical variable.

The lessR default version of a pie chart is the doughnut or ring chart.

    Chart(Dept, type="pie")

The doughnut or ring chart appears easier to read than a standard pie chart. But `Chart()` also can create the “old-fashioned” pie chart by setting the value of parameter `hole` to `0`. We have seen the summary statistics several times now, so turn off the output to the R console here with the `quiet` parameter.

    Chart(Dept, hole=0, type="pie")

Standard pie chart of variable Dept in the d data frame.

Set the size of the hole in the doughnut or ring chart with the parameter `hole`, which specifies the proportion of the pie occupied by the hole. The default hole size is 0.65. Set that value to 0 to close the hole.

### Other Hierarchical Charts

Some charts provide the perspective of the whole divided into its component pieces. The previously presented pie chart is one such example. These charts are called hierarchical charts or part-whole visualizations. Other possibilities are the treemap and icicle charts.

    Chart(Dept, type="treemap")

Treemap of count of the number of employees in each department.

    Chart(Dept, type="icicle")

Icicle chart of count of the number of employees in each department.

### Other Charts

    Chart(Dept, type="radar")

Radar chart of the count of the number of employees in each department.

Bubble chart of the count of the number of employees in each department.

## Specify the Numerical Variable

One possibility begins with the values of the \(x\) and \(y\) variables, such as in a table, and then create the bar chart directly from this summary table. To do so, enter the paired data values into a data file such as with Excel, and then read into R with `Read()`. When calling `Chart()`, specify the categorical \(x\) variable and then the numerical \(y\) variable.

When the numeric variable is specified, the data are a summary (pivot) table, with one row for each level of the categorical variable plotted. For example, suppose a summary table contains the departments and the mean salary for each department. Obtain the summary table with the lessR `pivot()` function (which has its own vignette). For the data frame d, calculate the mean of numerical variable Salary across levels of the categorical variable Dept.

    a <- pivot(d, mean, Salary, Dept)
    a

    ##   Dept  n na Salary_mean
    ## 1 ACCT  5  0    71792.78
    ## 2 ADMN  6  0    91277.12
    ## 3 FINC  4  0    79010.68
    ## 4 MKTG  6  0    80257.13
    ## 5 SALE 15  0    88830.07
    ## 6 <NA>  1  0    63772.58

The general syntax follows for processing this form of the data follows.

The bar chart follows, with the aggregated data stored in the data frame named a, so explicitly identify with the `data` parameter. For only one variable analyzed, the computed mean of the Salary variable in the a data frame from the previous call to `pivot()` is named mean by default.

    Chart(Dept, y=Salary_mean, data=a)

### Statistical Transformation of \(y\)

As seen, by default in the absence of other information, `Chart()` defines the numerical variable plotted as the count the occurrence of each level. You can define other statistical transformations of the numerical value of \(y\) with the `stat` parameter. Possible values of `stat`: `"sum"`, `"mean"`, `"sd"`, `"dev"`, `"min"`, `"median"`, and `"max"`. The `"dev"` value displays the mean deviations to further facilitate a comparison among levels.

Here the \(x\)-variable is Dept, and \(y\)-variable is Salary. Display bars for values of `dev` <= 0 in a different color than values above with the `fill_split` parameter set at `0`. Do an ascending sort with the `sort` parameter set at `"+"`.

    Chart(Dept, y=Salary, stat="dev", sort="+", fill_split=0)

Compare this visualization of the mean deviations with the previous visualization of the means for each Dept.

Or, continuously blend the colors with a divergent scale.

    Chart(Dept, y=Salary, stat="deviation", sort="+",
             fill_scaled=TRUE, fill=c("red", "blue"))

### Stacked Bar or Bubble Charts

Following are stacked bar charts for 20 6-pt Likert scale items. The default scale is divergent from “browns” to “blues”.

    d <- Read("Mach4", quiet=TRUE)

    Chart(m01:m20, horiz=TRUE, labels="off", sort="+")

Following are stacked bubble charts for 20 6-pt Likert scale items, also referred to here as a (BPFM).

    Chart(m01:m20, type="bubble")

## Chart of Two Categorical Variables

### Standard Bar Charts

Specify the second categorical variable with the `by` parameter. Usually, for clarity, specify the `by` parameter by name. The general syntax follows.

The example plots Dept with the percentage of Gender divided in each bar.

    Chart(Dept, by=Gender)

### Sunburst Chart

The sunburst start is a hierarchical pie chart, that is, a pie chart with additional levels for each by variable.

    Chart(Dept, by=Gender, type="pie")

Specify additional rings for the sun burst chart by specifying multiple `by` variables.

    Chart(Dept, by=c(Gender, Plan), type="pie")

### Other Hierarchical Charts

Some charts provide the perspective of the whole divided into its component pieces. The previously presented pie chart is one such example. These charts are called hierarchical charts or part-whole visualizations. Other possibilities are the treemap and icicle charts.

    Chart(Dept, by=Gender, type="treemap")

Treemap of count of the number of employees in each department.

    Chart(Dept, by=Gender, type="icicle")

Icicle chart of count of the number of employees in each department.

### Other Charts

    p <- Chart(Dept, by=Gender, type="radar")
    p

    p <- Chart(Dept, by=Gender, type="bubble")
    p

    p <- Chart(Dept, type="dot")
    p

    p <- Chart(Dept, y=c(Pre, Post), stat="mean", origin_x=70, type="dot")
    p

### Trellis or Facet Plots

Can also do a Trellis chart with the `facet` parameter.

    Chart(Dept, facet=Gender)

Or, stack the charts vertically by specifying one column with the `n_col` parameter.

    Chart(Dept, facet=Gender, n_col=1)

### 100% Stacked Bar Chart

Obtain the 100% stacked version with the `stack100` parameter. This visualization is most useful for comparing levels of the `by` variable across levels of the `x` variable, here Dept, when the frequencies in each level of the `x` variable differ. The percentages across categories are compared instead of the counts. The percentage for each column, then, sums to 100%.

    Chart(Dept, by=Gender, stack100=TRUE)

### Long Value Labels

Long value labels on the horizontal axis are also addressed by moving to a new line whenever a space is encountered in the label. Here read responses to the Mach IV Machiavellianism scale where each item is scored from 0 to 5.

    d <- rd("Mach4", quiet=TRUE)

Also, read variable labels into the l data frame, which are then used to automatically label the output, both the visualization and text output to the console.

    l <- rd("Mach4_lbl", quiet=TRUE)

Convert the specified four Mach items to ordered factors with the lessR function `factors()`. This function implements the base R function `factor()` across a range of variables instead of a single variable (without needing other function calls). A response of 0 is a Strongly Disagree, etc.

    LikertCats <- c("Strongly Disagree", "Disagree", "Slightly Disagree",
                         "Slightly Agree", "Agree", "Strongly Agree")
    d <- factors(c(m06,m07,m09,m10), levels=0:5, labels=LikertCats, ordered=TRUE)

Because the factors are defined as ordered with the `factors()` function, the colors are plotted in a sequential scale, from light to dark. Because output to the console has been turned off in general, turn back on just for this analysis because of new data.

    Chart(m06, by=m07, quiet=FALSE)

    ## [Interactive chart from the Plotly R package (Sievert, 2020)]
    ##
    ## >>> Suggestions  or  enter: style(suggest=FALSE)
    ## Chart(m06, by=m07, type="radar")  # Plotly radar chart
    ## Chart(m06, by=m07, type="treemap")  # Plotly treemap chart
    ## Chart(m06, by=m07, type="pie")  # Plotly pie/sunburst chart
    ## Chart(m06, by=m07, type="icicle")  # Plotly icicle chart
    ## Chart(m06, by=m07, type="bubble")  # Plotly bubble chart
    ##

    ## m06: Honesty is the best policy in all cases
    ##   - by levels of -
    ## m07: There is no excuse for lying to someone else
    ##
    ## Joint and Marginal Frequencies
    ## ------------------------------
    ##
    ##                    m06
    ## m07                 Strongly Disagree Disagree Slightly Disagree Slightly Agree Agree Strongly Agree Sum
    ##   Strongly Disagree                 4        3                 2              3     3              2  17
    ##   Disagree                          7       24                 7              6    18              2  64
    ##   Slightly Disagree                 4       14                30             13    24              2  87
    ##   Slightly Agree                    2        1                10             16    12              2  43
    ##   Agree                             0        3                13              5    56             16  93
    ##   Strongly Agree                    1        2                 1              1     8             34  47
    ##   Sum                              18       47                63             44   121             58 351
    ##
    ## Cramer's V: 0.380
    ##
    ## Chi-square Test of Independence:
    ##      Chisq = 253.103, df = 25, p-value = 0.000
    ## >>> Low cell expected frequencies, chi-squared approximation may not be accurate

If the categorical variable is not a factor, use a parameter `fill` plural color such as `"blues"`, `"reds"`, or `"emaralds"` to assign a gradient. See the Customize vignette for more details on color palettes.

## Customization

### One Categorical Variable

#### Custom Colors

Specify a single fill color with the `fill` parameter, the edge color of the bars with `color`. Set the transparency level with `transparency`. Against a lighter background, display the value for each bar with a darker color using the `labels_color` parameter. To specify a color, use color names, specify a color with either its `rgb()` or `hcl()` color space coordinates, or use the lessR custom color palette function `getColors()`.

    Chart(Dept, fill="darkred", color="black", transparency=.8,
             labels_color="black")

Use the `theme` parameter to change the entire color theme: “colors”, “lightbronze”, “dodgerblue”, “slatered”, “darkred”, “gray”, “gold”, “darkgreen”, “blue”, “red”, “rose”, “green”, “purple”, “sienna”, “brown”, “orange”, “white”, and “light”. In this example, changing the full theme accomplishes the same as changing the fill color. Turn off the displayed value on each bar with the parameter `labels` set to `off`. Specify a horizontal bar chart with base R parameter `horiz`.

    Chart(Dept, theme="gray", labels="off", horiz=TRUE)

Or, you can use `style()` to change the theme for subsequent visualizations as well. See the `Customize` vignette.

Dept is not an ordinal variable (i.e., with ordered values set by the base R `factor()` function). Ordinal variables plot by default with a range of the same hue from light to dark. To illustrate, you can choose many different sequential palettes from `getColors()`: “reds”, “rusts”, “browns”, “olives”, “greens”, “emeralds”, “turquoises”, “aquas”, “blues”, “purples”, “violets”, “magentas”, and “grays”.

    Chart(Dept, fill="reds")

The color-blind family of viridis palettes are also available: “viridis”, “cividis”, “magma”, “inferno”, “plasma”. The bar graph below indicates the primary viridis palette.

    Chart(Dept, fill="viridis")

For something different, many Wes Anderson movie themes are available: “BottleRocket1”, “BottleRocket2”, “Rushmore1”, “Rushmore”, “Royal1”, “Royal2”, “Zissou1”, “Darjeeling1”, “Darjeeling2”, “Chevalier1”, “FantasticFox1”, “Moonrise1”, “Moonrise2”, “Moonrise3”, “Cavalcanti1”, “GrandBudapest1”, “GrandBudapest2”, “IsleofDogs1”, “IsleofDogs2”.

    Chart(Dept, fill="GrandBudapest1")

Instead of arbitrarily setting the value of the interior color of the bars with the `fill` parameter, map the value of the tabulated count to the bar `fill`. With mapping, the color of the bars depends upon the bar height. The higher the bar, the darker the color. Specify `(count)` as the fill color to map the values of the numerical variable to the fill color.

    Chart(Dept, fill=(count))

    ## >>> Now set to TRUE the more general parameter:  fill_scale.

#### Axis Labels

##### Rotate Labels

Rotate and offset the axis labels with `rotate_x` and `offset` parameters. Do a descending sort of the categories by frequencies with the `sort` parameter.

    Chart(Dept, rotate_x=45, offset=1, sort="-")

##### Format Labels

The default for formatting both axis labels is to round numeric values of thousands, such as 100000 to 100K. With parameter `axis_fmt`, this default of to `{"K"}` can be changed. Also you can specify `{","}` to insert commas in large numbers with a decimal point or `{"."}` to insert periods, or `{""}` to turn off formatting. The value of `{"K"}` can also be combined with `{","}` or `{"."}` by forming a vector of values, such as `c("K", ",")`.

Axis labels can also be formatted by adding a prefix to a numeric value with the parameter `axis_pre`, such as `$` or `€`. The value of `axis_pre` can be multiple characters, such as for the Brazilian currency, `R$`.

    Chart(Dept, axis_fmt=",", axis_y_pre="£")

### Two Categorical Variables

The stacked version is default, but the values of the second categorical variable can also be represented with bars, more helpful to compare the values with each other. Here, put the legend on the top with the `labels_position` parameter set to `"out"`.

    Chart(Dept, by=Gender, beside=TRUE, labels_position="out")

Or, display the bars horizontally with the `horiz` parameter set to `TRUE`.

    Chart(Gender, by=Dept, horiz=TRUE)

Specify two custom fill colors for Gender.

    Chart(Dept, by=Gender, fill=c("deepskyblue", "black"))

### Annotation

Annotate a plot with the `add` parameter. To add a rectangle use the `"rect"` value of `add`. Here set the rectangle around the message centered at <3,10>. To specify a rectangle requires two corners of the rectangle, `<x1,y1>` and `<x2,y2>`. To specify text requires just a single coordinate, `<x1,y1>`. With the `add` parameter, the message follows the specification of `"rect"`, so the coordinates of the text message follow the coordinates for the rectangle.

First lighten the fill color of the annotation with the `add_fill` parameter for the `style()` function.

    d <- Read("Employee", quiet=TRUE)

    style(add_fill="aliceblue")
    Chart(Dept, add=c("rect", "Employees by\nDepartment"),
                         x1=c(1.75,3), y1=c(11, 10), x2=4.25, y2=9)

### Variable Labels

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

    ##                                                label
    ## Years                     Time of Company Employment
    ## Gender                                  Man or Woman
    ## Dept                             Department Employed
    ## Salary                           Annual Salary (USD)
    ## JobSat            Satisfaction with Work Environment
    ## Plan             1=GoodHealth, 2=GetWell, 3=BestCare
    ## Pre    Test score on legal issues before instruction
    ## Post    Test score on legal issues after instruction

### Filter Rows

Many tools exist in the standard R environment and with functions from additional packages to manipulate the data within data frames. An example is sub-setting the data by rows. For example, create a subset data frame of the employee data set that only consists of women. Or, perform the filtering with lessR functions such as `Chart()`. To do so, invoke the parameter `filter` and either specify a logical condition such as `Gender=="W"` or specify an integer vector that corresponds to the wrong numbers of the data frame you wish to retain.

    Chart(Dept, filter=(Gender=="W"))

    Chart(Dept, filter=c(1:5, 20,21))

## Interactive Bar Chart

An interactive visualization lets the user in real time change parameter values to change characteristics of the visualization. To create an interactive bar chart that displays the corresponding parameters, run the function `interact()` with the value `"BarChart"` specified.

    interact("BarChart")

The 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 `Chart()`. Simply enter a question mark followed by the name of the function.

    ?BarChart

## More

More on Bar Charts 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.