O'Reilly logo

R Graph Essentials by David Alexander Lillis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required