Labeling points with text

Now we will see how to label points with text. Suppose that we want a graph of the heights of treatment A female patients against their weight before treatment, in which each point is labeled by the patient's name and where the text is in red. First we subset using the subset() command, but we include two criteria (gender and treatment). We include the text using the function geom_text(). Remember that the variable PATIENT gave the names of each patient. Enter the following syntax:

F <- subset(T, GENDER == "F" & TREATMENT == "A")
S <- ggplot(F, aes(x=HEIGHT, y=WEIGHT_1, label=PATIENT)) 

Finally, we add the required text, but we do not include the points as yet:

S + geom_text(size = 6, col = "red")  

The following graph shows the names of the patients:

Labeling points with text

In the preceding graph, the patients' names appeared without any points. Of course, we can set text aesthetics to our chosen values. As an exercise, use the following syntax to create a graph with point labels. You use blue text and position the text underneath and to the left of the points using hjust= 1 and vjust= 1:

 S + geom_point() + geom_text(hjust=1, vjust=1, size = 6, col = "blue")  

Note that one name was cut off (Ann). We will learn how to fix that problem in the next example. The arguments hjust and vjust vary the position of the text relative to the points; hjust allows you to control horizontal justification, while vjust allows you to control vertical justification. Both hjust and vjust range between 0 and 1, where 0 gives left-justified text and 1 produces right-justified text. Try different values of hjust and vjust yourself. For example, using the value zero places the text above and to the right.

You can also experiment with the text size and angle. In the following example, you set hjust and vjust to zero:

S + geom_point() + geom_text(angle = 45, hjust=0, vjust=0, size = 6, col = "darkgreen")  

If you created this graph, you will have noticed that two names were cut off (Mary and Sue). To fix this problem, we can reset the axis limits to include both names using scale_x_continuous() and scale_y_continuous(). We choose axis limits that ensure the inclusion of both names. For the vertical axis, we can choose 50 Kg to 100 Kg, and for the horizontal axis we can choose 140 cm to 200 cm.

S + geom_point() + geom_text(angle = 45, hjust=0, vjust=0, size = 6, col = "darkgreen") +  scale_y_continuous(limits=c(50, 100)) + scale_x_continuous(limits=c(140, 200)) 

Here is our graph:

Labeling points with text

Now each name appears in full. Of course, we could have chosen other axis limits that included all of the patients' names.

Mapping color to text

Next, we will learn how to map color to text for categorical variables. In this example, you map text color to the variable ETH. Again, you do so within aes() by turning ETH into a factor:

S + geom_text(aes(color=factor(ETH))) 

You will see that the patient's names now appear in a different color for each ethnicity. For the next example, we put the text at an angle of 35 degrees and justify the text. We retain suitable axis limits to include all names in full. We also choose a color scheme using scale_color_brewer(). Finally, we rename and relabel the legend entries appropriately using name and labels. The syntax is as follows:

S + geom_point() + geom_text(aes(color=factor(ETH), angle = 35, hjust=1, vjust=1)) +  scale_y_continuous(limits=c(50, 90)) + scale_x_continuous(limits=c(140, 190)) + scale_color_brewer(palette= "Set1" , name = "Ethnicity",labels=c("European","Asian","Other")) 

This syntax will give the following graph:

Mapping color to text

Mapping text color to the categorical variable ETH has conveyed additional information about these patients.

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

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