Mapping aesthetics to categorical variables

Now let's map both symbol size and shape to GENDER. To map symbol size to levels of a categorical variable, it is helpful to set the variable as a factor using the factor() command.

Then you set up your plot as before, but control your symbol size by adding a new layer using the plus sign: + scale_size_manual(values = c(a, b)).

The parameters a and b have a minimum value of 0 and can be as large as you like. You must select values of a and b to produce symbols of the desired size. In the next example, I have chosen symbol sizes of 5 and 7. You may select different sizes, depending on your preferences. You will gain experience very quickly and select the symbol sizes that suit your graphs best. In this case, I introduced some transparency using the alpha = I() syntax. Transparency assists in the interpretation of graphs that involve a large number of points. Enter the following syntax:

qplot(HEIGHT, WEIGHT_1, data = T, xlab = "HEIGHT (cm)", ylab = "WEIGHT BEFORE TREATMENT (kg)" , size = factor(GENDER), color = factor(GENDER), alpha = I(0.7)) + scale_size_manual(values = c(5, 7))

You get this graph:

Mapping aesthetics to categorical variables

Our graph looks good, but the legend title includes the word factor. We shall see how to fix this problem in a later example. For now, enter the following syntax:

qplot(HEIGHT, WEIGHT_1, data = T, xlab = "HEIGHT (cm)", ylab = "WEIGHT BEFORE TREATMENT (kg)" , size = factor(GENDER), color = factor(GENDER), alpha = I(0.7)) 

We mapped the size and color to one variable (in this case GENDER), but we can map each of these aesthetics to a different factor variable. Let's map the symbol size to one variable (GENDER) and color to another variable (EXERCISE) using the arguments size and color.

qplot(HEIGHT, WEIGHT_1, data = T, geom = c("point"), xlab = "HEIGHT (cm)", ylab = "WEIGHT BEFORE TREATMENT (kg)" , size = factor(GENDER), color = factor(EXERCISE)) + scale_size_manual(values = c(5, 7))

Now you get a graph like this one:

Mapping aesthetics to categorical variables
..................Content has been hidden....................

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