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


When we want to visualize more than one summarized numeric variable over the category of another variable, we need to produce a stacked bar chart. In this case, each bar represents summary statistics of each variable. The different variables occupy different heights with various colors in that single bar.

Getting ready

To produce stacked bar charts, we will use the same summarized data that we created in the Creating bar charts recipe.

How to do it…

The command that produces the stacked bar chart is exactly the same as the simple bar chart. The only difference is that we need a new argument, which is stack=TRUE:

barchart(disA+disB+disC~factor(age),data=dis_dat,stack=TRUE)

How it works…

The first argument is the formula that specifies the variables to be displayed in the plot. The left-hand side indicates how many components there will be in each of the bars. The right-hand side indicates the grouping information. The stack=TRUE argument makes sure that the bar plot will...