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 a box plot


A box plot is another important graph that summarizes the data along with the distribution. In this recipe, we will produce a box plot to visualize the data summary with the distribution using the ggplot2 implementation.

Getting ready

Recall ggplotdata for this recipe; we will use the disB numeric variable over the category of the sex variable in order to produce a box plot.

How to do it...

The primary code to produce a boxplot is as follows:

ggplot(data=ggplotdata,aes(y=disB,x=sex))+geom_boxplot()

The following figure shows us the visual output of the preceding code:

How it works…

The main visualization is produced by the geom_boxplot() part of the function. The ggplot function takes the argument of the data frame that we want to use, along with the x- and y-axis variable names. The geom part maps the data and produces the actual visualization.