Creating stacked bar charts

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.

Getting ready

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)

How to do it...

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 ...

Get R: Data Analysis and Visualization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.