Book Image

R Graph Essentials

Book Image

R Graph Essentials

Overview of this book

Table of Contents (11 chapters)
R Graph Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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:

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