← Back to R Course | Chapter 10: Data Visualization | Lesson 3 of 7

barplot()

A bar plot draws one bar per category, with taller bars for bigger values, like comparing the heights of your friends. It makes counts easy to compare.

In this page:

  1. barplot()
Syntax
r
barplot(heights, names.arg = labels, main = "title")

barplot()

barplot(heights) draws a bar chart from a vector of values (or a table() result), one bar per value, useful for comparing categorical counts or totals.

names.arg labels each bar. It's the standard way to visualize categorical/count data, as opposed to hist() which is for continuous numeric distributions.

Note: barplot(table(x)) is a common combo: tabulate a categorical vector, then chart the counts directly.

Example: barplot()

markup
sales <- c(Jan = 100, Feb = 150, Mar = 120)

print(sales)
cat("barplot(sales) would draw one bar per month with these heights\n")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Passing raw data like c("a", "b", "a") and getting an error, when you need barplot(table(x)) for counts.
  2. Forgetting names.arg when the vector has no names, so bars are unlabeled.
  3. Confusing barplot() with hist(), when barplot shows one bar per category.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.