Creating bar charts with more than one factor variable
In this first recipe, we will learn how to make bar charts for data with more than one category. Such bar charts are commonly used to compare values of the same measure across different categories.
Getting ready
We will use the base library barplot()
function, but we will also use the RColorBrewer
package to choose a good color palette. So, let's first install and load that package:
install.packages("RColorBrewer") #if not already installed library(RColorBrewer)
How to do it...
Let's reuse the citysales.csv
example dataset that we used in the first chapter:
citysales<-read.csv("citysales.csv") barplot(as.matrix(citysales[,2:4]), beside=TRUE, legend.text=citysales$City, args.legend=list(bty="n",horiz=TRUE), col=brewer.pal(5,"Set1"), border="white",ylim=c(0,100), ylab="Sales Revenue (1,000's of USD)", main="Sales Figures") box(bty="l")
![](https://static.packt-cdn.com/products/9781783988785/graphics/8785OS_05_01.jpg)
How it works...
The key argument for drawing bar charts with more than one category is the beside
argument...