Data Science

Explain R-Bar charts with diagrams

 Using bar chart we can represent a categorical variable easily. Bar chart basically represent the data in rectangular bars with length of the bar proportional to the value of the variable.

The basic syntax of creating a bar chart in R is –

barplot(x, xlab, ylab, col, xlim, ylim, main, ………     )

x is the vector of matrix containing the categorical variable.

xlab  is the level of the x-axis, must be a string.

ylab  is the level of the y-axis, must be a string.

col  uses for changing the colour of the diagram.

xlim defines the range of the x axis, must be a vector.

ylim defines the range of the y axis, must be a vector.

main is the title of the diagram, must be a string.

We can also make the bars in the bar diagram horizontal by using command horiz=TRUE.

Example: We use the BOD dataset from R, for this example. If we view the data then we can see than there are some demands recorded over time.


If we want to plot the demands over time, we can use bar diagram, It is to note that here time is working as a categorical variable.



Now, using the other commands in barplot() we can customize our graph.



We can create a bar plot using ggplot2 .

For this we have to install a package ggplot2, and then using the following codes we can execute the bar chart.



Comments