简体   繁体   English

如何在散点图中标记特定点

[英]How to label specific points in scatterplot

I have a scatterplot using this data.frame : 我有一个使用此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] . 我只想标记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 我认为您只需要在with()中将df子集化,请尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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