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


Bar charts are the most common data visualization for categorical data. However, we can also produce bar charts for summarized numeric variables over the category of other variables. In this recipe, we will see how to produce a bar chart that summarizes numeric variables over the category of other variables.

Getting ready

To create the bar chart, we will simulate a dataset with three numeric variables and one categorical variable for the purpose of grouping. The three numeric variables will indicate the incubation period of three different diseases—say, disease A, B, and C—in weeks. The categorical variable will indicate four different age groups, for example, 1 indicates age 0-1 year, 2 indicates 1-5 years, 3 indicates 5-10 years, and 4 indicates over 10 years. Here is the code that produces the dataset:

# Set a seed value to make the data reproducible
set.seed(12345)
data_barchart <-data.frame(disA=rnorm(n=100,mean=20,sd=3),
                disB=rnorm(n=100,mean=25...