Controlling colors on your graph

Now let's map the symbol size to GENDER and the symbol color to EXERCISE. To control your symbol colors, use the layer scale_color_manual() and select your desired colors. We choose red and blue and symbol sizes 3 and 7, as shown in the following syntax:

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(3, 7)) + scale_color_manual(values = c("red","blue"))

Here is our graph with red and blue points:

Controlling colors on your graph

Now you know how to choose you own color scheme for your qplot graphs. Mapping color to categorical data can give us additional insight into the relationships that exist between variables.

Now let's see how to control the legend title (the title that sits directly above the legend). For this example, we control the legend title through the name argument within the two functions scale_size_manual() and scale_color_manual(). Enter the following syntax:

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(3, 7), name="Gender") + scale_color_manual(values = c("red","blue"),name="Exercise")

Our graph now includes a better legend title:

Controlling colors on your graph

By including the arguments name="Gender" and name="Exercise" in the relevant function, we were able to control the legend title and include the variable names without the word factor. In the examples of the remainder of this chapter, we will omit this technique in order to simplify the syntax presented with each example.

Now let's create a similar graph, but including transparency using alpha = I(). We choose a value of 0.3 to illustrate the effect of transparency quite clearly. Enter the following syntax:

qplot(HEIGHT, WEIGHT_1, data = T, alpha = I(0.3), geom = c("point"), xlab = "HEIGHT (cm)", ylab = "WEIGHT BEFORE TREATMENT (kg)" , size = factor(GENDER), color = factor(EXERCISE)) + scale_size_manual(values = c(3, 7), name="Gender") + scale_color_manual(values = c("red","blue"),name="Exercise")

The output is as follows:

Controlling colors on your graph

We can control transparency using either decimals or fractions. Rather than I(0.7), we could use I(7/10).

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

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