Bonono Bonono - 3 months ago 14
R Question

How to label specific points in scatterplot

I have a scatterplot using this


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]))


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