Bar Plots in R
Bar Plots in R
Introduction
A bar plot (or bar chart) is a graphical representation of categorical data using rectangular bars whose heights or lengths are proportional to the values they represent.
Bar plots are useful for:
- Comparing quantities
- Displaying frequencies
- Showing survey results
- Comparing sales figures
- Visualizing marks and populations
In R, bar plots are created using the barplot() function.
Syntax
barplot(height,
names.arg,
main,
xlab,
ylab,
col,
horiz,
border)
where:
- height : vector or matrix containing values.
- names.arg : labels for bars.
- main : title.
- xlab : x-axis label.
- ylab : y-axis label.
- col : color of bars.
- horiz : horizontal bar plot.
- border : border color.
Example 1: Simple Bar Plot
Suppose sales of four products are:
| Product | Sales |
|---|---|
| A | 25 |
| B | 40 |
| C | 35 |
| D | 50 |
sales <- c(25,40,35,50)
barplot(sales)
Output:
Four bars are drawn with heights 25, 40, 35, and 50.
Example 2: Adding Labels and Title
Example 3: Changing Colors
sales <- c(25,40,35,50)
barplot(sales,
names.arg=c("A","B","C","D"),
col="blue")
Different Colors
sales <- c(25,40,35,50)
barplot(sales,
names.arg=c("A","B","C","D"),
col=c("red","blue","green","orange"))
Each bar gets a different color.
Example 4: Horizontal Bar Plot
sales <- c(25,40,35,50)
barplot(sales,
names.arg=c("A","B","C","D"),
horiz=TRUE,
col="cyan")
horiz=TRUE produces horizontal bars.
Example 5: Border Color
sales <- c(25,40,35,50)
barplot(sales,
names.arg=c("A","B","C","D"),
col="yellow",
border="red")

Example 6: Changing Width of Bars
Example 7: Displaying Values on Bars
sales <- c(25,40,35,50)
x <- barplot(sales,
names.arg=c("A","B","C","D"),
col="lightblue",ylim=c(0,60))
text(x,
sales,
labels=sales,
pos=3)
text() places the values above the bars.
Example 8: Student Marks
Example 9: Population of Cities
Example 10: Grouped Bar Plot
Suppose sales of two products over four years are:
sales <- matrix(c(
20,25,30,35,
15,20,25,30),
nrow=2,
byrow=TRUE)
barplot(sales,
beside=TRUE,
names.arg=c("2021","2022","2023","2024"),
col=c("blue","red"))
legend("topleft",
legend=c("Product A","Product B"),
fill=c("blue","red"))
beside=TRUE places bars side-by-side.
Example 11: Stacked Bar Plot
sales <- matrix(c(
20,25,30,35,
15,20,25,30),
nrow=2,
byrow=TRUE)
barplot(sales,
col=c("blue","red"))
Default behavior is stacked bars.
Example 12: Adding Grid Lines
Example 13: Space Between Bars
Example 14: Using Data Frames
Example 15: Frequency Distribution
Important Parameters
| Parameter | Purpose |
|---|---|
height | Heights of bars |
names.arg | Labels |
main | Title |
xlab | X-axis label |
ylab | Y-axis label |
col | Color |
border | Border color |
horiz | Horizontal bars |
beside | Side-by-side bars |
space | Space between bars |
width | Width of bars |
Types of Bar Plots
| Type | Description |
|---|---|
| Simple Bar Plot | One variable |
| Horizontal Bar Plot | Horizontal bars |
| Colored Bar Plot | Different colors |
| Grouped Bar Plot | Multiple categories |
| Stacked Bar Plot | Values stacked vertically |
| Frequency Bar Plot | Using table() |
Applications
- Student marks analysis
- Product sales comparison
- Population studies
- Frequency distributions
- Survey results
- Election statistics
- Department-wise employee counts
- Monthly rainfall comparison
Difference Between Histogram and Bar Plot
| Bar Plot | Histogram |
|---|---|
| Categorical data | Continuous data |
| Bars separated by spaces | Bars touch each other |
Created using barplot() | Created using hist() |
| Order not important | Order important |
Conclusion
The barplot() function is one of the most widely used plotting functions in R for comparing categorical data. By changing parameters such as col, horiz, beside, and space, different styles of bar plots can be created. Grouped and stacked bar plots are especially useful for comparing multiple datasets simultaneously.









Comments
Post a Comment