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:
Syntax
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()
sales <- c(Jan = 100, Feb = 150, Mar = 120)
print(sales)
cat("barplot(sales) would draw one bar per month with these heights\n")
Login to try C/C++/Java/PHP code in the editor
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
- Passing raw data like
c("a", "b", "a")and getting an error, when you needbarplot(table(x))for counts. - Forgetting
names.argwhen the vector has no names, so bars are unlabeled. - Confusing
barplot()withhist(), when barplot shows one bar per category.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: