📊

Creating Bar Graphs with R Programming

Mar 20, 2025

Lecture Notes: Constructing a Bar Graph with R

Overview

  • Purpose: To learn how to construct a bar graph showing proportions using R programming.
  • Example Context: Population in Park City categorized into children, working-age adults, and retirees.

Understanding the Data

  • Frequency Table:
    • Displays number of people in each category (frequency).
    • Shows percentage each category makes up (relative frequency).

Constructing the Bar Graph

Key Components Needed

  1. Categories

    • Used to label each bar.
    • Example: Age groups (children, working-age adults, retirees).
  2. Heights

    • Heights correspond to the proportions of each category.
    • Example: Proportion percentages from the table.

Using R for Bar Graph

  • Use barplot() function to construct the graph.

Steps to Create Bar Graph in R

  1. Define Heights: Proportions List

    • Use c() to combine values into a list.
    • Values from the last column of the table (proportions):
      • 19.11%, 43.37%, 37.52%
    • Assign to a variable: proportions
  2. Define Labels: Age Groups List

    • Use c() to create list of age groups.
    • Ensure values are in double quotes: "children", "working age adults", "retirees".
    • Assign to a variable: ageGroups
  3. Construct Bar Plot

    • Call barplot() function:
      • Heights parameter: proportions
      • Labels parameter: ageGroups
    • Ensure correct order alignment between the heights and names lists.

Considerations

  • Use double quotes for text labels to avoid computation errors.
  • Maintain consistent order in lists for accurate plotting.

Outcome

  • R creates a bar chart based on provided data:
    • Bars labeled: Children, Working-age adults, Retirees.
    • Heights: Close to expected proportions (19.11%, 43.37%, 37.52%).

Conclusion

  • By giving the computer the necessary data, it can automate the construction of a bar chart.
  • Understanding how to prepare data and use R functions are key steps in visual data representation.