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


A bar chart is the simplest chart that displays the frequency of a categorical variable or the summary statistics of a numeric variable over the category of other variables. In this recipe, we will learn how we can produce a bar chart using the ggplot2 library.

Getting ready

Let's call the ggplotdata dataset that was created in the preceding section. We intend to produce a bar chart that will represent the mean of a numeric variable on the y axis over the category of the economic status variable on the x-axis. So, we need to prepare the summarized dataset as follows:

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

The description of the most important components of the preceding function is as follows:

  • The ddply()function takes the data frame as the input and produces the output in the data frame as well. In the function name, the first d signifies the input data frame and...