Book Image

R Graph Essentials

Book Image

R Graph Essentials

Overview of this book

Table of Contents (11 chapters)
R Graph Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating boxplots


In this section, we will learn how to create boxplots. Using the medical dataset, let's produce a simple boxplot of patient weight before treatment, grouped by the three levels of treatment. We use the syntax geom = "boxplot" and again we choose a nice color from the Hexadecimal Color Chart. Enter the following syntax:

qplot(TREATMENT, WEIGHT_2, data = T,   geom = "boxplot",   xlab = " TREATMENT", ylab = "WEIGHT (kg)", fill = I("#99CCFF"))

Here is the boxplot:

If we do not wish to create a grouped boxplot and want a boxplot of all of the data taken together, we simply omit the categorical variable from the qplot() command. Anyway, within any boxplot, we can include the data as points—positioned according to the levels of the factor variable. Simply include "point" within geom as follows:

qplot(TREATMENT, WEIGHT_2, data = T, geom = c("boxplot","point"),xlab = "TREATMENT", ylab = "WEIGHT (kg)", fill =I("#669900"))

The boxplot, along with the raw data, looks like this:

Including...