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 multiple bar charts


In some situations, we need to produce bar charts for more than one numeric variable over the category of other variables. In this recipe, we will learn how we can produce a bar chart with more than one numeric variable.

Getting ready

To produce multiple bar charts, we will use the same ggplotdata source data, but we need some preprocessing to create the input data for the plot. Firstly, we will summarize the source data and calculate the mean for each numeric variable for each unique value of economic status variable:

library(plyr)
bardata <- ddply(ggplotdata,.(econ_status),summarize,meandisA=mean(disA),
meandisB=mean(disB),meandisC=mean(disC),meandisD=mean(disD))

In ggplot2, we need to transform the data in a layout where the variable names will be a value of a certain variable in order to produce multiple bar charts; this is sometimes called tidy data. Tidy data means that each row will contain information related to one variable and one observation. So, we...