Book Image

R Graphs Cookbook Second Edition

Book Image

R Graphs Cookbook Second Edition

Overview of this book

Table of Contents (22 chapters)
R Graphs Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Adjusting the orientation of bars – horizontal and vertical


In this recipe, we will learn how to adjust the orientation of bars to horizontal or vertical.

Getting ready

We will use the same dataset we used in the last few recipes (citysales.csv) and the RColorBrewer color palette package.

How to do it...

Let's make a bar chart with horizontal bars:

barplot(as.matrix(citysales[,2:4]), beside=TRUE,horiz=TRUE,
legend.text=citysales$City, args.legend=list(bty="n"),
col=brewer.pal(5,"Set1"),border="white",
xlim=c(0,100), xlab="Sales Revenue (1,000's of USD)",
main="Sales Figures")

How it works...

In the example, we set the horiz argument to TRUE, which makes the bars horizontal. By default, horiz is set to FALSE, making the bars vertical. While it's really easy to make the bars horizontal, we must remember that the axes are reversed when we do that. So, in the example, we had to set the limits for the x axis (xlim instead of ylim) and set xlab (instead of ylab) to "Sales Revenue". We also removed the...