Stacked Bar Chart in R
A stacked bar chart extends the standard bar chart from looking at numeric values across one categorical variable to two. Each bar in a standard bar chart is divided into a number of sub-bars stacked end to end, each one corresponding to a level of the second categorical variable. This article discusses how one can be created using R.
The function used here to create a stacked bar chart is barplot().
Syntax: barplot(H,xlab,ylab,main, names.arg,col)
- H: is a vector or matrix containing numeric values used in a bar chart.
- xlab: is the label for the x-axis.
- ylab: is the label for the y-axis.
- main: is the title of the bar chart.
- names.arg: is a vector of names appearing under each bar.
- col: is used to give colors to the bars in the graph.
Returns: a barplot.
- Create data
- Pass the data to the barplot() function
- Pass appropriate arguments to the function
- Display plot