Book Image

R Graph Essentials

Book Image

R Graph Essentials

Overview of this book

This book is targeted at R programmers who want to learn the graphing capabilities of R. This book will presume that you have working knowledge of R.
Table of Contents (6 chapters)
5
Index

Creating facet plots


If you have a large dataset that includes a categorical variable, you can use the facets command to produce multiple graphs: one for each level of the categorical variable. In the following example, we will create a graph for each level of ETH (1, 2, and 3) using facets = ETH ~ . Note the tilde sign followed by the period. Enter this syntax:

qplot(HEIGHT, WEIGHT_1, data = T,  geom = "point",  main = "HEIGHT VS. WEIGHT BY ETHNICITY", xlab = "WEIGHT BEFORE TREATMENT (Kg)", ylab = "HEIGHT (cm)", facets = ETH ~ .)

Our facet plot is as follows:

Indeed, we have three scatterplots arranged vertically: one for each level of ETHNICITY. We can also create facet plots across the levels of two factor variables. In the following code, we will create a scatterplot of HEIGHT against WEIGHT_1 for each combination of SMOKE and EXERCISE in each facet, where the two levels of gender are represented by shape and color. Here is the required command:

qplot(HEIGHT, WEIGHT_1, data=T, shape=factor...