O'Reilly logo

R: Data Analysis and Visualization by Ágnes Vidovics-Dancs, Kata Váradi, Tamás Vadász, Ágnes Tuza, Balázs Árpád Szucs, Julia Molnár, Péter Medvegyev, Balázs Márkus, István Margitai, Péter Juhász, Dániel Havran, Gergely Gabler, Barbara Dömötör, Gergely Daróczi, Ádám Banai, Milán Badics, Ferenc Illés, Edina Berlinger, Bater Makhabel, Hrishi V. Mittal, Jaynal Abedin, Brett Lantz, Tony Fischetti

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required