International College of Digital Innovation, CMU
June 24, 2026
A bar plot (or bar chart) is a type of graph that represents categorical data with rectangular bars.
Each bar’s length or height is proportional to the value it represents.
Bar plots are commonly used to compare the sizes of different categories, making it easy to visualize and interpret differences between groups.
Key Features of a Bar Plot:
Categories on One Axis: Typically, the categories are plotted along the x-axis (horizontal axis) in a vertical bar plot. In a horizontal bar plot, the categories are plotted along the y-axis.
Bar Length/Height: The length or height of each bar corresponds to the value of the category it represents. The higher or longer the bar, the greater the value.
Spacing Between Bars: Bars are usually separated by spaces to distinguish between different categories.
Customizable: Bar plots can be customized in various ways, including the color of the bars, the orientation (vertical or horizontal), whether the bars are stacked or grouped, and the use of additional elements like labels, legends, and grid lines.
Types of Bar Plots:
Vertical Bar Plot: Bars are vertical, with categories on the x-axis and values on the y-axis.
Horizontal Bar Plot: Bars are horizontal, with categories on the y-axis and values on the x-axis.
Stacked Bar Plot: Bars are stacked on top of each other to show sub-group values within the same category.
Grouped Bar Plot: Bars for different sub-groups are placed next to each other, making it easier to compare sub-group values within each category.
Uses of Bar Plots:
Comparing categories: Bar plots are ideal for comparing the size of different categories within a dataset.
Visualizing distributions: They can show the distribution of a categorical variable.
Highlighting trends: Bar plots can help highlight trends or patterns in categorical data over time or across different groups.
This function can generate bar plots in two primary ways:
By counting the occurrences of each category in a dataset (the default behavior).
By plotting pre-summarized data where the height of the bars represents the values provided.
Explanation:
aes(x = class): Maps the class variable to the x-axis.
geom_bar(): Automatically counts the number of occurrences for each class.
If you already have summarized data (e.g., counts), you can use geom_bar(stat = "identity").
Explanation
aes(x = group, y = count): Maps grou to the x-axis and count to the y-axis.
geom_bar(stat = "identity"): Uses the provided count values directly.
A stacked bar plot shows the distribution of a second categorical variable within each bar.
Explanation
aes(fill = drv): Fills the bars based on the drv (drive type) variable.To create grouped bars instead of stacked bars, use position = "dodge" or position = "dodge2".
Explanation:
position = "dodge": Places bars for each group side by side instead of stacking them.
position = "dodge2": Similar to “dodge”, but with slightly more spacing between the bars.
You can customize the colors of the bars using scale_fill_manual() or other color scales.
Remark: The left-hand side shows the class types in the variable class, and the right-hand side shows the color names.
To flip the axes and create a horizontal bar plot, use coord_flip().
Explanation:
coord_flip(): Swaps the x and y axes to create a horizontal bar plot.You can use facets to create multiple bar plots for different subsets of data.
Explanation:
facet_grid(.~ drv): Creates a separate plot for each level of drv.you can reorder the factor levels of the variable mapped to the x-axis. This can be done using the reorder() function within aes().
Explanation:
reorder(class, -table(class)[class]): Reorders the levels of class based on the frequency of each level in descending order.
The - sign before table(class)[class] sorts the bars in descending order.
If we have a bar plot where the y-axis represents a continuous variable, you can sort the bars based on that variable.
Explanation:
reorder(group, -count): Reorders the group variable based on count in descending order.
stat = "identity": Tells ggplot2 to use the provided value directly.
Complete the script using a piped data workflow to generate a fundamental bar chart that displays the categorical count frequency of vehicle classes.
viewof var_ch11_ex1_basic_bar_plot_1 = html`<input type="text" class="ojs-hidden-ch11_ex1_basic_bar_plot" data-ojs-proxy="ch11_ex1_basic_bar_plot::var_ch11_ex1_basic_bar_plot_1" value="">`
viewof var_ch11_ex1_basic_bar_plot_2 = html`<input type="text" class="ojs-hidden-ch11_ex1_basic_bar_plot" data-ojs-proxy="ch11_ex1_basic_bar_plot::var_ch11_ex1_basic_bar_plot_2" value="">`
viewof var_ch11_ex1_basic_bar_plot_run = html`<input type="number" class="ojs-hidden-ch11_ex1_basic_bar_plot" data-ojs-run="ch11_ex1_basic_bar_plot" value="0">`Modify the baseline bar plot configuration by injecting localized aesthetic properties to alter the fill color of the discrete segments.
viewof var_ch11_ex2_bar_plot_custom_colors_1 = html`<input type="text" class="ojs-hidden-ch11_ex2_bar_plot_custom_colors" data-ojs-proxy="ch11_ex2_bar_plot_custom_colors::var_ch11_ex2_bar_plot_custom_colors_1" value="">`
viewof var_ch11_ex2_bar_plot_custom_colors_run = html`<input type="number" class="ojs-hidden-ch11_ex2_bar_plot_custom_colors" data-ojs-run="ch11_ex2_bar_plot_custom_colors" value="0">`Complete the bar plot configuration to map a secondary categorical grouping attribute to fill mechanics and arrange the bars side-by-side using a dodge positional adjustment configuration.
viewof var_ch11_ex3_grouped_bar_plot_1 = html`<input type="text" class="ojs-hidden-ch11_ex3_grouped_bar_plot" data-ojs-proxy="ch11_ex3_grouped_bar_plot::var_ch11_ex3_grouped_bar_plot_1" value="">`
viewof var_ch11_ex3_grouped_bar_plot_run = html`<input type="number" class="ojs-hidden-ch11_ex3_grouped_bar_plot" data-ojs-run="ch11_ex3_grouped_bar_plot" value="0">`Complete the plotting configuration to map the structural sub-categorical drive groupings into distinct vertical stacks within each vehicle classification bar.
viewof var_ch11_ex4_stacked_bar_plot_1 = html`<input type="text" class="ojs-hidden-ch11_ex4_stacked_bar_plot" data-ojs-proxy="ch11_ex4_stacked_bar_plot::var_ch11_ex4_stacked_bar_plot_1" value="">`
viewof var_ch11_ex4_stacked_bar_plot_run = html`<input type="number" class="ojs-hidden-ch11_ex4_stacked_bar_plot" data-ojs-run="ch11_ex4_stacked_bar_plot" value="0">`Complete the plotting configuration to invert the Cartesian grid coordinate system, transforming default vertical bars into a horizontal distribution layout.
viewof var_ch11_ex5_horizontal_bar_plot_1 = html`<input type="text" class="ojs-hidden-ch11_ex5_horizontal_bar_plot" data-ojs-proxy="ch11_ex5_horizontal_bar_plot::var_ch11_ex5_horizontal_bar_plot_1" value="">`
viewof var_ch11_ex5_horizontal_bar_plot_run = html`<input type="number" class="ojs-hidden-ch11_ex5_horizontal_bar_plot" data-ojs-run="ch11_ex5_horizontal_bar_plot" value="0">`Complete the plotting configuration to dynamically restructure the categorical variable order, sorting the categorical bars in a descending frequency layout.
viewof var_ch11_ex6_bar_plot_sorted_1 = html`<input type="text" class="ojs-hidden-ch11_ex6_bar_plot_sorted" data-ojs-proxy="ch11_ex6_bar_plot_sorted::var_ch11_ex6_bar_plot_sorted_1" value="">`
viewof var_ch11_ex6_bar_plot_sorted_run = html`<input type="number" class="ojs-hidden-ch11_ex6_bar_plot_sorted" data-ojs-run="ch11_ex6_bar_plot_sorted" value="0">`Complete the script to modify the semantic metadata layers of the visualization by overriding the default scale labels and title attributes.
viewof var_ch11_ex7_bar_plot_custom_labels_1 = html`<input type="text" class="ojs-hidden-ch11_ex7_bar_plot_custom_labels" data-ojs-proxy="ch11_ex7_bar_plot_custom_labels::var_ch11_ex7_bar_plot_custom_labels_1" value="">`
viewof var_ch11_ex7_bar_plot_custom_labels_run = html`<input type="number" class="ojs-hidden-ch11_ex7_bar_plot_custom_labels" data-ojs-run="ch11_ex7_bar_plot_custom_labels" value="0">`Complete the plotting block to generate structural facet groupings, partitioning the baseline categorical vehicle bar profiles into horizontal matrix channels using the drive attribute.
viewof var_ch11_ex8_bar_plot_facets_1 = html`<input type="text" class="ojs-hidden-ch11_ex8_bar_plot_facets" data-ojs-proxy="ch11_ex8_bar_plot_facets::var_ch11_ex8_bar_plot_facets_1" value="">`
viewof var_ch11_ex8_bar_plot_facets_2 = html`<input type="text" class="ojs-hidden-ch11_ex8_bar_plot_facets" data-ojs-proxy="ch11_ex8_bar_plot_facets::var_ch11_ex8_bar_plot_facets_2" value="">`
viewof var_ch11_ex8_bar_plot_facets_run = html`<input type="number" class="ojs-hidden-ch11_ex8_bar_plot_facets" data-ojs-run="ch11_ex8_bar_plot_facets" value="0">`Complete the plotting block to map a discrete vector variable directly onto explicit custom fill color arguments.
viewof var_ch11_ex9_bar_plot_manual_colors_1 = html`<input type="text" class="ojs-hidden-ch11_ex9_bar_plot_manual_colors" data-ojs-proxy="ch11_ex9_bar_plot_manual_colors::var_ch11_ex9_bar_plot_manual_colors_1" value="">`
viewof var_ch11_ex9_bar_plot_manual_colors_run = html`<input type="number" class="ojs-hidden-ch11_ex9_bar_plot_manual_colors" data-ojs-run="ch11_ex9_bar_plot_manual_colors" value="0">`The gglikert() function from the ggstats package provides a convenient way to visualize Likert-scale survey data using the grammar of graphics.
It produces clean, publication-ready plots that show the distribution of responses across multiple items.
A Likert scale is commonly used to measure attitudes, perceptions, and agreement levels.
Examples:
gglikert() is designed specifically for this type of ordered categorical data.
Automatically detects ordered factors
Displays stacked bar charts for each item
Provides percentage labels
Supports customization through ggplot2 themes
Works well in tidyverse pipelines
We can sort the plot with sort.
By default, the plot is sorted based on the proportion being higher than the center level, i.e. in this case the proportion of answers equal to “Agree” or “Strongly Agree”. Alternatively, the questions could be transformed into a score and sorted accorded to their mean.
We can reverse the order of the answers with reverse_likert = TRUE.
Proportion labels could be removed with add_labels = FALSE.
or customized.
By default, Likert plots will be centered, i.e. displaying the same number of categories on each side on the graph. When the number of categories is odd, half of the “central” category is displayed negatively and half positively.
It is possible to control where to center the graph, using the cutoff argument, representing the number of categories to be displayed negatively: 2 to display the two first categories negatively and the others positively; 2.25 to display the two first categories and a quarter of the third negatively
All code and content from ggstat package.