简体   繁体   中英

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

You could do

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

在此处输入图片说明

I think you just need to subset df in your with() , try

set.seed(1)    
df <- data.frame(a=sample(1:10,10), b = sample(1:10,10),ID = sample(letters[1:10]))
plot(a~b,data = df)
with(df[1:5,], text(a~b, labels = ID [1:5]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM