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

```{r include=FALSE}
suppressPackageStartupMessages(library("lessR"))
```


## Data

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.

```{r read}
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.Currently, necessarily read the label file into the _l_ data frame. 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.

```{r labels}
l <- rd("Employee_lbl")
l
```


## Bar Chart of One Variable

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 a bar chart function contains, at a minimum, the name of the categorical variable with the categories to be plotted. With the `BarChart()` 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 `BarChart()` with a categorical variable named $x$. 

```{r bc1, dataTable, echo=FALSE, out.width='28%', fig.asp=.7, fig.align='center', out.extra='style="border-style: none"'}
knitr::include_graphics(system.file("img", "bcExplain.png", package="lessR"))
```

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


### Default Bar Chart

Consider the categorical variable Dept in the Employee data table.  Use `BarChart()` 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.

```{r bcEx, fig.width=4, fig.height=3.5, fig.align='center', fig.cap="Bar chart of tablulated counts of employees in each department."}
BarChart(Dept)
```

The default color theme, `"colors"`, fills the bars in the bar chart with with different hues (according to the default qualitative palette). See more explanation of this and related color palettes in the vignette _Customize_.

`BarChart()` 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.

We do not need to see this output to the R console repeated again for different bar charts of the same data, so turn off for now with the parameter `quiet` set to `TRUE`. Can set this option for each call to `BarChart()`, or can set as the default for subsequent analyses with the `style()` function.

```{r}
style(quiet=TRUE)
```



### 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()`.

```{r fig.width=4, fig.height=3.75, fig.align='center'}
BarChart(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`.

```{r fig.width=4, fig.height=3.5, fig.align='center'}
BarChart(Dept, theme="gray", labels="off", horiz=TRUE)
```

Or, 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, can choose many different sequential palettes from `getColors()`: "reds", "rusts", "browns", "olives", "greens", "emeralds", "turquoises", "aquas", "blues", "purples", "violets", "magentas", and "grays". 

```{r fig.width=4, fig.height=3.5, fig.align='center'}
BarChart(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.

```{r fig.width=4, fig.height=3.5, fig.align='center'}
BarChart(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".

```{r fig.width=4, fig.height=3.5, fig.align='center'}
BarChart(Dept, fill="GrandBudapest1")
```


### 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.


```{r  fig.width=4, fig.height=3.5, fig.align='center'}
BarChart(Dept, rotate_x=45, offset=1, sort="-")
```


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.

```{r fig.width=4, fig.height=3.5, fig.align='center'}
BarChart(Dept, fill=(count))
```


### 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 `BarChart()`, 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.

```{r a}
a <- pivot(d, mean, Salary, Dept)
a
```


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

```{r bcXY, dataTable, echo=FALSE, out.width='35%', fig.asp=.7, fig.align='center', out.extra='style="border-style: none"'}
knitr::include_graphics(system.file("img", "bcXYExplain.png", package="lessR"))
```

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. 

```{r xy, fig.width=4, fig.height=3.5, fig.align='center'}
BarChart(Dept, Salary_mean, data=a)
```

### Statistical Transformation of $y$

As seen, by default in the absence of other information, `BarChart()` defines the numerical variable plotted as the count the occurrence of each level. 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 `"+"`.

```{r, fig.width=4, fig.height=3.5, fig.align='center'}
BarChart(Dept, Salary, stat="dev", sort="+", fill_split=0)
```

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


### 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.

```{r, fig.width=4, fig.height=3.5, fig.align='center'}
style(add_fill="aliceblue")
BarChart(Dept, add=c("rect", "Employees by\nDepartment"),
                     x1=c(1.75,3), y1=c(11, 10), x2=4.25, y2=9)
```



## 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.

```{r, echo=FALSE, include=FALSE}
d <- Read("Employee")
```

```{r pc1, fig.align='center', fig.width=3.5, fig.height=3.5}
PieChart(Dept)
```

The doughnut or ring chart appears easier to read than a standard pie chart. But the __lessR__ function `PieChart()` 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.


```{r hole0, fig.width=3.5, fig.height=3.5, fig.align='center', fig.cap="Standard pie chart of variable Dept in the _d_ data frame."}
PieChart(Dept, hole=0, quiet=TRUE)
```

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.


## Bar Chart of Two Variables

### Standard Bar Charts

Specify the second categorical variable with the `by` parameter. Specify the `by` parameter by name. The general syntax follows.

```{r bc2var, dataTable, echo=FALSE, out.width='34%', fig.asp=.7, fig.align='center', out.extra='style="border-style: none"'}
knitr::include_graphics(system.file("img", "bc2Explain.png", package="lessR"))
```

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

```{r, fig.width=4, fig.align='center'}
BarChart(Dept, by=Gender)
```


Specify two custom fill colors for _Gender_.

```{r, fig.width=4, fig.align='center'}
BarChart(Dept, by=Gender, fill=c("deepskyblue", "black"))
```

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"`.

```{r, fig.width=5, fig.align='center'}
BarChart(Dept, by=Gender, beside=TRUE, labels_position="out")
```


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

```{r, fig.width=6}
BarChart(Gender, by=Dept, horiz=TRUE)
```



### Trellis Plots

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

```{r, fig.width=4, fig.align='center'}
BarChart(Dept, by1=Gender)
```

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

```{r, fig.align='center'}
BarChart(Dept, by1=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%.

```{r, fig.width=4, fig.align='center'}
BarChart(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. 

```{r}
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.

```{r}
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.
 
```{r}
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. 

```{r fig.width=6, fig.height=4.5, fig.align='center'}
BarChart(m06, by=m07, quiet=FALSE)
```

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. 


## Multiple Variable Bar Chart

A single bar chart can be constructed for multiple variables. This visualization is particularly useful when all the variables are measured on the same scale, such as self-report responses to 6-pt Likert items as shown in the previous example of the 20-item Mach 4 scale. By default the individual variables are sorted by their respective means.

```{r mult, fig.width=6, fig.height=5}
d <- rd("Mach4", quiet=TRUE)
BarChart(m01:m20)
```



## 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 `BarChart()`. 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.
