Bonono Bonono - 1 month ago 7
R Question

How to label specific points in scatterplot

I have a scatterplot using this

data.frame
:

set.seed(1)
df <- data.frame(a=sample(1:10,10), b = sample(1:10,10),ID = sample(letters[1:10]))


I only want to label the points of
ID [1:5]
.

I tried the code below but it still labelled all of them

plot(a~b,data = df)

with(df,text(a~b, labels = ID [1:5]))

Answer

You could do

with(df[df$ID %in% df$ID[c(1:5)],],text(x = b, y = a, labels = ID, pos = 2))

enter image description here

Comments