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 with vertical error bars


Bar charts with error bars are commonly used in analyzing and reporting results of scientific experiments. In this recipe, we will learn how to add error bars to a bar chart in a similar way to the recipe for scatter plots in Chapter 4, Creating Scatter Plots.

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

One change we will make in this recipe is that we will use the transpose of the citysales dataset (turns rows into columns and columns into rows). So, first let's create the transpose as a new dataset:

sales<-t(as.matrix(citysales[,-1]))
colnames(sales)<-citysales[,1] 

Now, let's make a bar plot with 5-percent-error bars that show the sales of the three products across the five cities as categories:

x<-barplot(sales...