且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何给剧情添加标签

更新时间:2023-12-04 23:38:22

为了跟进Andrie的优秀答案,我经常使用两种方法为如果我需要突出显示特定数据,则在图上的点的子集。两者都在下面进行了演示:

$ $ $ $ $ $ $ $ c $ dat $ data.frame(x = rnorm(10),y = rnorm(10) ,label = letters [1:10])

#创建要标记的数据子集。在这里,我们标记点a -e
labeled.dat< -dat [dat $ label%in%letters [1:5],]

ggplot(dat,aes(x,y ))+ geom_point()+
geom_text(data = labeled.dat,aes(x,y,label = label),hjust = 2)
$ b $#或者为每个图层添加一个单独的图层点你想要标签。
ggplot(dat,aes(x,y))+ geom_point()+
geom_text(data = dat [dat $ label ==c,],aes(x,y,label = label ),hjust = 2)+
geom_text(data = dat [dat $ label ==g,],aes(x,y,label = label),hjust = 2)


Is there a way to add labels to each point in a plot? I did this on an image editor just to convey the idea: 1.

The original one was generated with:

qplot(pcomments, gcomments , data = topbtw, colour = username)

To follow up on Andrie's excellent answer, I frequently employ two methods to add labels to a subset of points on a plot if I need to highlight specific data. Both are demonstrated below:

dat <- data.frame(x = rnorm(10), y = rnorm(10), label = letters[1:10])

#Create a subset of data that you want to label. Here we label points a - e
labeled.dat <- dat[dat$label %in% letters[1:5] ,]

ggplot(dat, aes(x,y)) + geom_point() +
  geom_text(data = labeled.dat, aes(x,y, label = label), hjust = 2)

#Or add a separate layer for each point you want to label.
ggplot(dat, aes(x,y)) + geom_point() +
  geom_text(data = dat[dat$label == "c" ,], aes(x,y, label = label), hjust = 2) + 
  geom_text(data = dat[dat$label == "g" ,], aes(x,y, label = label), hjust = 2)