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