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

Get R: Data Analysis and Visualization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.