[英]how to add labels to a plot
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. 为了跟进Andrie的优秀答案,如果我需要突出显示特定数据,我经常使用两种方法将标签添加到绘图上的一个子集中。 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)
Yes, use geom_text() to add text to your plot. 是的,使用geom_text()为您的绘图添加文本。 Here is an example: 这是一个例子:
library(ggplot2)
qplot(mtcars$wt, mtcars$mpg, label=rownames(mtcars), geom="text")
ggplot(mtcars, aes(x=wt, y=mpg, label=rownames(mtcars))) + geom_text(size=3)
See the on-line documentation for more information: http://had.co.nz/ggplot2/geom_text.html 有关更多信息,请参阅联机文档: http : //had.co.nz/ggplot2/geom_text.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.