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:

Creating facet plots

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(GENDER), color=factor(GENDER),facets=SMOKE ~ EXERCISE, size=I(3), xlab="HEIGHT", ylab="WEIGHT BEFORE TREATMENT", main = "HEIGHT vs. WEIGHT")

You will get the following output:

Creating facet plots

Since SMOKE and EXERCISE are both categorical variables of two levels, we ended up with four graphs—one for each combination of the two levels. Note the syntax facets=SMOKE ~ EXERCISE.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset