Page 255 - Data Science class 11
P. 255
This also prints a chart separately, as shown below.
7.5 Bar chart or Bar graph
A bar chart is a pictorial representation of data that presents categorical data with rectangular bars whose heights
or lengths are proportional to the values that they represent. In other words, it is the pictorial representation of a
dataset. These data sets contain the numerical values of variables that represent the length or height.
A bar chart represents data in rectangular bars, with the length or height of the bar proportional to the value of the
variable. R uses the function barplot() to create bar charts. R can draw both vertical and horizontal bars in the bar
chart. In a bar chart, each of the bars can be given different colours.
Syntax:
R uses the function barplot() to create bar charts. Here, both vertical and horizontal bars can be drawn.
Syntax:
barplot(H, xlab, ylab, main, names.arg, col)
Parameters:
• H: This parameter is a vector or matrix containing numeric values that are used in the bar chart.
• xlab: This parameter is the label for x axis in bar chart.
• ylab: This parameter is the label for y axis in bar chart.
• main: This parameter is the title of the bar chart.
• names.arg: This parameter is a vector of names appearing under each bar in bar chart.
• col: This parameter is used to give colours to the bars in the graph.
Example
A simple bar chart is created using just the input vector and the name of each bar.
Entering the below script will create and save the bar chart in the current R working directory.
# Creating the data for the chart
H <- c(6,13,25,3,42)
# Giving the chart file a name
png(file = "barchart.png")
# Plotting the bar chart
Coding for Data Science Visualisation using R-Studio 253

