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:

ProductSales
A25
B40
C35
D50
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

sales <- c(25,40,35,50)

barplot(sales,
names.arg=c("A","B","C","D"),
main="Product Sales",
xlab="Products",
ylab="Sales")



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

sales <- c(25,40,35,50)

barplot(sales,
width=c(1,2,1,2),
col="green")




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

marks <- c(85,90,75,95,80)

barplot(marks,
names.arg=c("John","Mary","Alex","Sara","David"),
main="Student Marks",
ylab="Marks",
col="orange")




Example 9: Population of Cities

population <- c(12,18,25,15)

barplot(population,
names.arg=c("Delhi","Mumbai","Chennai","Kolkata"),
col="purple",
main="City Population")



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

sales <- c(25,40,35,50)

barplot(sales,
col="lightgreen")

grid()




Example 13: Space Between Bars

sales <- c(25,40,35,50)

barplot(sales,
space=1.5,
col="pink")




Example 14: Using Data Frames

student <- data.frame(
Name=c("John","Mary","Alex","Sara"),
Marks=c(85,92,78,95)
)

barplot(student$Marks,
names.arg=student$Name,
col="skyblue",
main="Student Marks")




Example 15: Frequency Distribution

grades <- factor(c("A","B","A","C","B","A","A"))

freq <- table(grades)

barplot(freq,
col="green",
main="Grade Distribution")




Important Parameters

ParameterPurpose
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

TypeDescription
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 PlotHistogram
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

Popular posts from this blog

Statistical Methods Lab ( R Language) PCCBL308 Semester 3 KTU BTech CB and CU 2024 Scheme - Dr Binu V P

Programs in R - using control statements - Assignment 2

Basic R Programs to Try - Assignment 1