Based Plot: Bar Plot

Published

November 5, 2024

Modified

September 24, 2025

Full Screen

Summary of R Commands Used

1. barplot(): Creates bar plots (vertical, horizontal, grouped, stacked).

NoteMain arguments:
  • height → numeric values (vector or matrix)

  • names.arg → labels for categories

  • col → bar colors (single color or vector of colors)

  • main → plot title

  • xlab, ylab → axis labels

  • horiz = TRUE → horizontal bars

  • beside = TRUE → grouped bar plot (side by side)

  • legend.text → adds a legend (when input is a matrix)

  • args.legend → position and formatting of legend

  • ylim / xlim → axis limits

  • border → bar border color

2. table()

  • Creates a frequency table (counts).

  • Works with vectors, factors, or data frames.

  • Can be combined with t() (transpose) to flip rows and columns.

3. t()

  • Transposes a table or matrix.

  • Used with table() before plotting → changes grouping orientation.

4. sort()

  • Orders numeric or factor levels before plotting.

  • Example: table(faculty) |> sort()

5. Pipe Operator (|>)

  • Passes results from one function to the next.

  • Example:

  faculty |> table() |> barplot()

6. box()

  • Adds a box (frame) around an existing plot.

  • Argument col changes the box color.

7. pie(): Creates pie charts.

NoteMain arguments:
  • x → numeric vector of values

  • labels → category names for slices

  • col → colors of slices

  • main → title