Stacked bar charts are another form of bar chart used to compare values across categories. As the name implies, the bars for each category are stacked on top of each other instead of being placed next to each other.
We will use the same dataset and color scheme as the last recipe, so ensure that you have the RColorBrewer
package installed and loaded:
install.packages("RColorBrewer") library(RColorBrewer)
Let's draw a stacked bar chart of sales figures across the five cities:
citysales<-read.csv("citysales.csv") barplot(as.matrix(citysales[,2:4]), legend.text=citysales$City, args.legend=list(bty="n",horiz=TRUE), col=brewer.pal(5,"Set1"),border="white", ylim=c(0,200),ylab="Sales Revenue (1,000's of USD)", main="Sales Figures")