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 bar widths, spacing, colors, and borders


In this recipe, we will learn how to adjust the styling of bars by setting their width, the space between them, colors, and borders.

Getting ready

We will continue using the citysales.csv example dataset in this recipe. Make sure that you have loaded it into R and type in the recipe at the R prompt. You might also want to save the recipe as a script so that you can easily run it again later.

How to do it...

Let's adjust all the arguments at once to make the same graph as in the Creating bar charts with more than one factor variable recipe but with different visual settings:

barplot(as.matrix(citysales[,2:4]), beside=TRUE,
legend.text=citysales$City, args.legend=list(bty="n",horiz=T),
col=c("#E5562A","#491A5B","#8C6CA8","#BD1B8A","#7CB6E4"),
border=FALSE,space=c(0,5),
ylim=c(0,100),ylab="Sales Revenue (1,000's of USD)",
main="Sales Figures")

How it works...

Firstly, we changed the colors of the bars by setting the col argument to a vector of five...