International College of Digital Innovation, CMU
June 23, 2026
viewof distType3 = Inputs.radio(
[
"Basic Bar Plot",
"Horizontal Bar Plot",
"Grouped Bar Plot",
"Stacked Bar Plot",
"100% Stacked Bar Plot",
"Diverging Bar Plot",
"Bar Plot with Error Bars",
"Sorted Bar Plot (Descending)",
"Bar Plot with Data Labels",
"Pareto Chart"
],
{ label: "Bar Plot Types", value: "Basic Bar Plot", inline: true }
)A bar plot is a type of chart or graph that uses bars to represent data values.
It is essentially a visual display of information where individual bars or columns correspond to different categories or groups, and the length or height of each bar reflects the magnitude of the data it represents.
Bar plots are commonly used for visualizing categorical data and making comparisons between different groups.
The bar plot components:
Axes:
The horizontal axis typically represents categories or groups.
The vertical axis represents the values, frequencies, or percentages associated with each category.
Bars:
Each bar is a rectangular column that extends from the baseline (zero) to a height corresponding to the value it represents.
The width of the bars may vary, but they are usually uniform.
Spacing:
bar plots are useful for showing the distribution of data, identifying patterns, and making comparisons between different categories.
In R, you can create a bar plot using the barplot() function. We will create a basic bar plot with random data:
Let’s break down the code:
vector:
values is a vector containing the corresponding values for each category.
categories is a vector containing the names of the categories.
argument:
height is the data you want to visualize. (numeric variable)
names.arg is used to specify the names of the categories. (character/factor variable)
col sets the color of the bars (you can choose any color).
main sets the title of the plot.
ylab and xlab set the labels for the y-axis and x-axis, respectively.
The table() function in R is used to create a contingency table, which shows the frequency distribution of a variable or the cross-tabulation of multiple variables.
It counts the number of occurrences of each unique value in a vector, factor, or set of factors.
Rotate graph by set horiz =TRUE
color: “#ffff00”
color: “#0000ff”
Use t() to create another stacked bar plot
Using t() flips the table, so now each gender groups the bars, making it easier to compare faculties within gender.
The argument beside = TRUE is used to create grouped bar plots, where bars corresponding to different categories are placed side by side, rather than stacked on top of each other.
color: “#ff0000”
color: “#00ff00”
color: “#0000ff”
Complete the executable script to compile categorical frequencies and render a baseline bar plot with descriptive axis labels.
viewof var_ex1_basic_bar_plot_1 = html`<input type="text" class="ojs-hidden-ex1_basic_bar_plot" data-ojs-proxy="ex1_basic_bar_plot::var_ex1_basic_bar_plot_1" value="">`
viewof var_ex1_basic_bar_plot_2 = html`<input type="text" class="ojs-hidden-ex1_basic_bar_plot" data-ojs-proxy="ex1_basic_bar_plot::var_ex1_basic_bar_plot_2" value="">`
viewof var_ex1_basic_bar_plot_3 = html`<input type="text" class="ojs-hidden-ex1_basic_bar_plot" data-ojs-proxy="ex1_basic_bar_plot::var_ex1_basic_bar_plot_3" value="">`
viewof var_ex1_basic_bar_plot_4 = html`<input type="text" class="ojs-hidden-ex1_basic_bar_plot" data-ojs-proxy="ex1_basic_bar_plot::var_ex1_basic_bar_plot_4" value="">`
viewof var_ex1_basic_bar_plot_run = html`<input type="number" class="ojs-hidden-ex1_basic_bar_plot" data-ojs-run="ex1_basic_bar_plot" value="0">`Complete the script to compute categorical cylinder frequencies and render a horizontal bar plot with customized aesthetic parameters.
viewof var_ex2_horizontal_bar_plot_1 = html`<input type="text" class="ojs-hidden-ex2_horizontal_bar_plot" data-ojs-proxy="ex2_horizontal_bar_plot::var_ex2_horizontal_bar_plot_1" value="">`
viewof var_ex2_horizontal_bar_plot_2 = html`<input type="text" class="ojs-hidden-ex2_horizontal_bar_plot" data-ojs-proxy="ex2_horizontal_bar_plot::var_ex2_horizontal_bar_plot_2" value="">`
viewof var_ex2_horizontal_bar_plot_run = html`<input type="number" class="ojs-hidden-ex2_horizontal_bar_plot" data-ojs-run="ex2_horizontal_bar_plot" value="0">`Complete the script to construct a multi-dimensional contingency table and render a grouped bar plot utilizing a segmented legend layout.
viewof var_ex3_grouped_bar_plot_1 = html`<input type="text" class="ojs-hidden-ex3_grouped_bar_plot" data-ojs-proxy="ex3_grouped_bar_plot::var_ex3_grouped_bar_plot_1" value="">`
viewof var_ex3_grouped_bar_plot_2 = html`<input type="text" class="ojs-hidden-ex3_grouped_bar_plot" data-ojs-proxy="ex3_grouped_bar_plot::var_ex3_grouped_bar_plot_2" value="">`
viewof var_ex3_grouped_bar_plot_run = html`<input type="number" class="ojs-hidden-ex3_grouped_bar_plot" data-ojs-run="ex3_grouped_bar_plot" value="0">`Complete the script to compute a contingency matrix and render a stacked bar plot deploying a customized segmented color palette.
viewof var_ex4_stacked_bar_plot_1 = html`<input type="text" class="ojs-hidden-ex4_stacked_bar_plot" data-ojs-proxy="ex4_stacked_bar_plot::var_ex4_stacked_bar_plot_1" value="">`
viewof var_ex4_stacked_bar_plot_2 = html`<input type="text" class="ojs-hidden-ex4_stacked_bar_plot" data-ojs-proxy="ex4_stacked_bar_plot::var_ex4_stacked_bar_plot_2" value="">`
viewof var_ex4_stacked_bar_plot_run = html`<input type="number" class="ojs-hidden-ex4_stacked_bar_plot" data-ojs-run="ex4_stacked_bar_plot" value="0">`Complete the script to aggregate species frequencies and render a highly customized bar plot with explicit mapping names and bar widths.
viewof var_ex5_bar_plot_custom_labels_colors_1 = html`<input type="text" class="ojs-hidden-ex5_bar_plot_custom_labels_colors" data-ojs-proxy="ex5_bar_plot_custom_labels_colors::var_ex5_bar_plot_custom_labels_colors_1" value="">`
viewof var_ex5_bar_plot_custom_labels_colors_2 = html`<input type="text" class="ojs-hidden-ex5_bar_plot_custom_labels_colors" data-ojs-proxy="ex5_bar_plot_custom_labels_colors::var_ex5_bar_plot_custom_labels_colors_2" value="">`
viewof var_ex5_bar_plot_custom_labels_colors_run = html`<input type="number" class="ojs-hidden-ex5_bar_plot_custom_labels_colors" data-ojs-run="ex5_bar_plot_custom_labels_colors" value="0">`viewof pieType = Inputs.radio(
[
"Basic Pie Chart",
"Colored Pie Chart",
"Pie Chart with Percentages",
"3D-like Pie Chart (Base Trick)",
"Pie Chart with Labels Outside",
"Donut Chart (Base R Style)",
"Pie Chart with Exploded Slice"
],
{ label: "Pie Chart Types", value: "Basic Pie Chart", inline: true }
)A pie chart is a circular statistical graphic that is divided into slices to illustrate numerical proportions. In R, you can create a pie chart using the pie() function.
See Listing 1 for details.
pie() Function:
The pie() function creates a pie chart.
The first argument x is the data you want to visualize, which in this case is the frequency of different cylinder types.
The main argument specifies the title of the chart.
The col argument is used to set custom colors for the slices of the pie chart.
The labels argument customizes the labels that appear on the slices of the pie chart.
The pie chart is one of the most popular charts — but also one of the most criticized in data visualization.
❌ Why pie charts are often not recommended:
🎯 Humans are bad at comparing angles
It’s hard for people to accurately compare the size of pie slices
especially when values are close.
Bar charts allow for easier and more accurate comparison using length, not angle.
Example: Can you easily tell the difference between 23% and 27% in a pie chart? Not really.
🍩 Too many slices = confusion
Pie charts get messy with more than 4–5 categories.
Colors become harder to distinguish.
Labels may overlap or require a legend, which makes interpretation slower.
📉 No clear axis = no easy comparison
There’s no y-axis or consistent baseline.
We lose the advantage of alignment (as in bar plots) which helps the eye compare quantities.
📊 Bar chart does everything better
Bar charts:
Are easier to read
Handle small differences better
Are simpler to label
Are more scalable to many categories
✅ When pie charts might still be acceptable:
| Use Case | Okay? |
|---|---|
| Only 2–3 clear categories | ✅ Yes |
| Audience is general/public | ✅ Maybe |
| Style or storytelling focus | ✅ Yes (e.g. in infographics) |
| Detailed analysis or comparison | ❌ No |
| Feature | Pie Chart | Bar Chart |
|---|---|---|
| Easy to compare | ❌ Hard | ✅ Easy |
| Best for small N | ✅ Yes (2–3 items) | ✅ Yes (scalable) |
| Labels/readability | ❌ Poor | ✅ Better |
| Style/visual appeal | ✅ Sometimes | ⚖️ Depends on use |
Edward Tufte (data viz pioneer): “The only thing worse than a pie chart is several of them.”
Stephen Few: Recommends bar or dot plots for almost every case where pie charts are used.