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

Creating bar charts


In this recipe, we will learn how to make bar plots that are useful to visualize summary data across various categories, such as sales of products or results of elections.

Getting ready

First, we need to load the citysales.csv example data file (you can download this file from the code download section of the book's companion website):

sales<-read.csv("citysales.csv",header=TRUE)

How to do it...

Just like the plot() function we used to make scatter plots and line graphs in the earlier recipes, the barplot() and dotchart() functions are part of the base graphics library in R. This means that we don't need to install any additional packages or libraries to use these functions.

We can make bar plots using the barplot() function as follows:

barplot(sales$ProductA,
names.arg= sales$City,
col="black")

The default setting of orientation for bars is vertical. To change the bars to horizontal, use the horiz argument (by default, it is set to FALSE):

barplot(sales$ProductA,
names.arg...